router

router-link

// 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始。

<router-link :to="{name:'home'}"> 
<router-link :to="{path:'/home'}"> //name,path都行, 建议用name 
//获取参数的方法和下面push()一样
 <router-link :to="{name:'home', params: {id:1}}"> 
<router-link :to="{name:'home', query: {id:1}}"> 

this.$router.push()

params传参
路由配置 path: "/home/:id" 或者path: "/home:id",不然刷新页面会取不到id(undefined)


this.$router.push({name:'home',params: {id:'1'}}) // 只能用 name
//html 取参
 $route.params.id
// script 取参
this.$route.params.id

query传参

 this.$router.push({name:'home',query: {id:'1'}}) //name
this.$router.push({path:'/home',query: {id:'1'}}) //path
// html 取参
 $route.query.id
// script 取参 
this.$route.query.id

总结
query类似 get, 跳转之后页面 url后面会拼接参数,类似?id=1, 非重要性的可以这样传, 密码之类还是用params
params类似 post, 跳转之后页面 url后面不会拼接参数 , 但是刷新页面id 会消失
this.$router.replace() this.$router.go(n)
this.$router.push跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面
this.$router.replace跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面 (就是直接替换了当前页面)
this.$router.go(n)向前或者向后跳转n个页面,n可为正整数或负整数

懒加载

const  aRounter  = ()=>import('@/components/routerCom/aRouter')

导航守卫

//路由独享的守卫
const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: Foo,
      beforeEnter: (to, from, next) => {
        // ...
      }
    }
  ]
})

//路由全局守卫
const router = new Router({.......})
//每次访问路由前,通过vuex中的actions中的异步方法确认是否登录状态
  router.beforeEach((to,from,next)=>{
  if(to.name != 'login' && to.name != 'index'){
    store.dispatch('me/checkMe')
  }
   next()
})

actions:{
        async checkMe({commit}){
            //请求/me接口,对登录信息进行判断,并保留状态
            const result = await Vue.prototype.$axiosGet('/me',{});
            if(!result){
                router.push({name:'login'})
                return
            }
            commit('changeLogin',{result})
        }
    }
补充

1.路由概念 2.动态路由 https://www.jianshu.com/p/a7484d9c43f0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。