vuerouter地址改变了页面没有跳转_VUE Router学习原理(二)

0185c3963532d8e08b946a4f6b14fb28.png

点击蓝字 关注我们

dffa7bb40479b35afea3dc07e1ab7564.png 593413751d9a8f7bf864ae91f7b2df79.png 32d1d46eca20ea194697a133933b1ea5.png

                        Vue Router

        一. 安装

        二. 导入

        三. 说明

        四. 使用

  4.1 静态页面跳转

                    4.2 动态页面跳转

                    4.3 子路由

                    4.4 通过程序传参

                    4.5 同时(同级)展示多个视图

                    4.6 导航钩子

0b07fa4d0422a646ff34e2a012967657.png 9e5efca0ef9153cc33e3747136bd9fc4.png

继VUE Router学习原理(一)后续

VUE Router学习原理(一)链接

https://mp.weixin.qq.com/s/p-Lghdp-pLse22LtY1vT8g

肆·使用

9ed18537143f322ad8943f2435191537.png a1cb8e5651e61d3a026b2611b3ff1828.png

4.4通过程序传参

·路由:在回退时回到上一层

·js

router.push(location, onComplete?, onAbort?)

·替换:替换掉当前的 history 记录, 在回退时不会回到上一层

·js

router.replace(location,onComplete?, onAbort?)

·回退

·js

router.go(n)

·带有参数的路由

·js

{
   name: 'usr'
   path: '/usr/:name',
   component: {
       template: `

I am {{ $route.params.name }}
more
`
   }
}
·js

router.push({ name: 'user', params: { userId: '123' }})

·带查询参数:/usr?usrname=private

·js

router.push({ path: 'usr', query: { usrname: 'AAA' }})

参考 官方网站

注意

this的指向

params中的属性名称必须存在

ac3eeb0e279c21f728104a530aa20f9f.png 9ed18537143f322ad8943f2435191537.png a1cb8e5651e61d3a026b2611b3ff1828.png

4.5同时(同级)展示多个视图

·案例:默认为 default

·html

·注册:使用components注册

·js

var router = new VueRouter({
   routes: [
       {
           path: '/',
           name: 'main',
           components: {
               default: {
                   template: `

首页

`
               }
               a: {
                   template: `

babaab

`
               },
               b: {
                   template: `

bcbcbc

`
               }
           }
       },
       xxx xxx xxx
});
a3c75168d53753fef3b2de6d29e77c77.png 9ed18537143f322ad8943f2435191537.png a1cb8e5651e61d3a026b2611b3ff1828.png

4.6导航钩子

·过滤器:没有登录时跳转到登录页面

·js

router.beforeEach(function(to, from, next){
   let logged_in = false;
   if(to.path == '/post' && !logged_in)
       next('/login'); // 跳转到登录页
   else
       next();         // 继续
});

·匹配:使用meta(元数据)完成判断

·js

routes: [
   {
       path: '/',
       name: 'main',
       meta: {
           login_required: true
       },
       components: {
           default: {
               template: `

首页

`
           }
       }
   },
   xxx xxx xxx
·js

router.beforeEach(function(to, from, next){
   let logged_in = false;
   if(!logged_in && to.matched.some(function(item){
           return item.meta.login_required;
   })) {
       next('/login'); // 跳转到登录页
   } else {
       next();         // 继续
   }
});

9ed18537143f322ad8943f2435191537.png a1cb8e5651e61d3a026b2611b3ff1828.png

画外音

VUE-Router学习笔记

更新完毕

759373a495d387e6aabb3ef89dc9e553.png fa1b1a584b28be6abc237bb627f4dec1.png

我知道你“在看”哟~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值