1. 路由初体验
效果类似掘金导航, 导航切换
1.1 引入路由文件
<script src="./vue-router.js"></script>
1.2 准备路由需要的组件
var index = Vue.component('index',{
template:'<div>首页</div>'
})
var productType = Vue.component('productType',{
template:'<div>这里显示商品编号</div>'
})
1.3 创建路由对象, 在这个对象里面配置路由规则
const router = new VueRouter({
})
1.4 通过routes属性配置路由规则,
他是一个数组, 数组中放的是对象, 每个对象对应一条规则,
并且每个对象里面都含有name(表示路由规则名称)/path(表示路径)/component(表示路径对应的组件)属性
const router = new VueRouter({
routes:[
{name:'index', path:'/index', component:index},
{name:'productType', path:'/product_type', component:productType}
]
})
1.5 在vue实例中注入路由, 这样整个应用程序都会拥有路由了
var vm = new Vue({
el: '#app',
// 5. 在vue实例中注入路由, 这样整个应用程序都会拥有路由了
router,
data: {
}
})
1.6 通过router-view挖坑, 路径匹配到的组件都会渲染到这个组件来
<div id="app">
<router-view></router-view>
</div>
1.7 vue路由中通过router-link去做跳转, 他有一个to属性, to属性的值必须和path中的值对应
router-link将来会被渲染成为a标签, 他的to属性会被渲染成a标签的href属性, 但它的值前面会加一个#,变为锚点
<div id="app">
<ul>
<li><router-link to="/index">首页</router-link></li>
<li><router-link to="/product_type">蔬菜</router-link></li>
<li><router-link to="/product_type">水果</router-link></li>
<li><router-link to="/product_type">肉类</router-link></li>
</ul>
<router-view></router-view>
</div>
2. 路由参数
2.1 router传递参数
<div id="app">
<ul>
<li><router-link to="/index">首页</router-link></li>
<li><router-link to="/product_type/11">蔬菜</router-link></li>
<li><router-link to="/product_type/22">水果</router-link></li>
<li><router-link to="/product_type/33">肉类</router-link></li>
</ul>
<router-view></router-view>
</div>
const router = new VueRouter({
routes:[
{name:'index', path:'/index', component:index},
{name:'productType', path:'/product_type/:id', component:productType}
]
})
2.2 接收参数
2.2.1 组件接收参数
在html中获取路由参数, 通过$route.params.参数名
var productType = Vue.component('productType',{
//在html中获取路由参数, 通过$route.params.参数名
template:'<div>这里显示商品编号{{$route.params.id}}</div>',
})
2.2.2 js接收参数
在js中获取路由参数, 通过this.$route.params.参数名
var productType = Vue.component('productType',{
//在html中获取路由参数, 通过$route.params.参数名
template:'<div>这里显示商品编号{{$route.params.id}}</div>',
//模板编译完成之后调用
mounted() {
//在js中获取路由参数, 通过this.$route.params.参数名
console.log(this.$route.params.id)
},
})
3. 响应路由参数的变化
提醒一下,当使用路由参数时,例如从 /user/foo 导航到 /user/bar,原来的组件实例会被复用。因为两个路由都渲染同个组件,比起销毁再创建,复用则显得更加高效。不过,这也意味着组件的生命周期钩子不会再被调用。
3.1 复用组件时,想对路由参数的变化作出响应的话,你可以简单地 watch (监测变化) $route 对象:
var productType = Vue.component('productType',{
data(){
return {
allProduct:''
}
},
//在html中获取路由参数, 通过$route.params.参数名
template:`
<div>
这里显示商品编号{{$route.params.id}}
<p>{{allProduct}}</p>
</div>
`,
//模板编译完成之后调用
mounted() {
//在js中获取路由参数, 通过this.$route.params.参数名
console.log(this.$route.params.id)
},
watch: {
//to表示你将要去的路由对象, from表示你从哪个路由来
'$route'(to,from){
console.log(to)
console.log(from)
if(to.params.id==='11'){
this.allProduct = '葫芦不等等'
}else if(to.params.id==='22'){
this.allProduct = '西瓜'
}else{
this.allProduct = '猪肉/牛肉'
}
}
}
})
3.2 或者使用 2.2 中引入的 beforeRouteUpdate 导航守卫:
const User = {
template: '...',
beforeRouteUpdate (to, from, next) {
// react to route changes...
// don't forget to call next()
}
}
4. 嵌套路由和编程式导航
嵌套路由
4.1 嵌套路由
4.1.1 嵌套路由创建
const router = new VueRouter({
routes:[
{name:'index', path:'/index', component:index},
{name:'productType', path:'/product_type/:id', component:productType,
children:[
//嵌套中的path不能加'/'
{name:'cookBook', path:'cook_book',component:cookBook}
]
}
]
})
var cookBook = Vue.component('cookBook]',{
template:'<div>我这里很多食谱, 欢迎查看</div>'
})
4.1.2 在被嵌套的组件中占坑''
var productType = Vue.component('productType',{
template:`
<div>
这里显示商品编号{{$route.params.id}}
<p>{{allProduct}}<button @click='jumpTo'>查看食谱</button></p>
<router-view></router-view>
</div>
`
})
4.2 编程式导航
通过 '<button @click='jumpTo'>查看食谱' 跳转
var productType = Vue.component('productType',{
template:`
<div>
这里显示商品编号{{$route.params.id}}
<p>{{allProduct}}<button @click='jumpTo'>查看食谱</button></p>
<router-view></router-view>
</div>
`
})
methods: {
jumpTo(){
//通过$router来跳转
this.$router.push({name:'cookBook'})
}
},
5. 路由重定向
重定向也是通过
routes配置来完成
{name:'default',path:'*',redirect: '/index'},
重定向的目标也可以是一个命名的路由:
{name:'default',path:'*',redirect: {name:'index'}}
甚至是一个方法,动态返回重定向目标:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: to => {
// 方法接收 目标路由 作为参数
// return 重定向的 字符串路径/路径对象
}}
]
})