這篇開始我們使用npm庫:vue-cli的開發方式的深入開發
這次我們只說一個東西:vue的路由處理以及路由方案:vue-router;
路由是啥,就是跳轉頁面的導航處理【我的個人理解,不喜勿噴】。
傳統方案很簡單:<code><a href="about.html">關于</a></code>
這里說白了就是告訴你怎么跳頁面[從A頁面到B頁面的切換]
1.使用腳手架初始化項目:
<code>vue init webpack pro</code>
2.根據下圖進行文件的增刪:
項目結構
index.html源碼:
----------------------------------代碼開始---------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tj1</title>
</head>
<body>
<div id="app">
<router-view></router-view>
</div>
</body>
</html>
----------------------------------代碼截止---------------------------------------
main.js源碼:
----------------------------------代碼開始---------------------------------------
import Vue from 'vue'
import VueRouter from 'vue-router'
import First from './components/First'
import Secod from './components/Secod'
Vue.use(VueRouter)
// 定義路由配置
const routes = [
{
path: '/',
component: First
},
{
path: '/secod',
component: Secod
}
]
// 創建路由實例
const router = new VueRouter({
routes
})
// 創建 Vue 實例
new Vue({
el: '#app',/* 對應index.html的div的id="app" */
data(){
return {
}
},
router
})
----------------------------------代碼截止---------------------------------------
First.vue源碼:
----------------------------------代碼開始---------------------------------------
<template>
<div>
<span>第1個頁面</span>
<router-link to="/secod" >測試</router-link>
</div>
</template>
<script>
export default {
name: 'first',
data () {
return {
}
}
}
</script>
<style scoped>
span{
font-weight: normal;
color:red
}
</style>
----------------------------------代碼截止---------------------------------------
Secod.vue源碼:
----------------------------------代碼開始---------------------------------------
<template>
<div>
<span>第2個頁面</span>
<router-link to="/" >測試</router-link>
</div>
</template>
<script>
export default {
name: 'secod',
data () {
return {
}
}
}
</script>
<style scoped>
span{
font-weight: normal;
color:red
}
</style>
----------------------------------代碼截止---------------------------------------