vue-router路由基础

1. 路由初体验

效果相似掘金导航, 导航切换 html

在这里插入图片描述
在这里插入图片描述

1.1 引入路由文件vue

<script src="./vue-router.js"></script>
复制代码

1.2 准备路由须要的组件react

var index = Vue.component('index',{
    template:'<div>首页</div>'
})
var productType = Vue.component('productType',{
    template:'<div>这里显示商品编号</div>'
})
复制代码

1.3 建立路由对象, 在这个对象里面配置路由规则vue-router

const router = new VueRouter({

})
复制代码

1.4 经过routes属性配置路由规则,编程

他是一个数组, 数组中放的是对象, 每一个对象对应一条规则, ​数组

而且每一个对象里面都含有name(表示路由规则名称)/path(表示路径)/component(表示路径对应的组件)属性app

const router = new VueRouter({
    routes:[
        {name:'index', path:'/index', component:index},
        {name:'productType', path:'/product_type', component:productType}
    ]
})
复制代码

1.5 在vue实例中注入路由, 这样整个应用程序都会拥有路由了ide

var vm = new Vue({
    el: '#app',
    // 5. 在vue实例中注入路由, 这样整个应用程序都会拥有路由了
    router,
    data: {

    }
})
复制代码

1.6 经过router-view挖坑, 路径匹配到的组件都会渲染到这个组件来ui

<div id="app">
	<router-view></router-view>
</div>
复制代码

1.7 vue路由中经过router-link去作跳转, 他有一个to属性, to属性的值必须和path中的值对应this

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 重定向的 字符串路径/路径对象
    }}
  ]
})
复制代码
相关文章
相关标签/搜索