首页 > 技术文章 > 快速理解 vue-router (vue路由)的使用方法(三)

carol1987 2019-03-06 21:52 原文

 

这是一个小例子,当用户登录之后,有些页面(包括子页面)是可以访问的

如果没登录的话,部分页面是没有权限访问的

 

这就用到了 vue-router 里的一个叫 meta 的属性

当我们设置了 meta 内的 mustLogin (必须登录)之后

后面所有的判断都可以依据这个属性来触发,非常方便

 

看代码吧

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="div1">
        <div>
            <router-link to="/home">首页</router-link>
            <router-link to="/login">登录</router-link>
            <router-link to="/post">贴子管理</router-link>
            <router-link to="/aaa">aaa的链接</router-link>
            
        </div>
        <div>
            <router-view></router-view>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/vue/2.6.0/vue.js"></script>
    <script src="https://cdn.bootcss.com/vue-router/2.6.0/vue-router.js"></script>

    <script>
        window.onload = function(){

            let routesArr = [
                {
                    path: '/home',
                    component: {
                        template:`<div>
                        <h1>这是首页</h1>
                        </div>`
                    }
                },{
                    path: '/login',
                    component: {
                        template:`<div>
                        <h1>登录页</h1>
                        </div>`
                    }
                },{
                    path: '/post',
                    meta: {
                        mustLogin: true
                    },
                    component: {
                        template:`<div>
                        <h1>贴子管理</h1>
                        <router-link to="rain" append>下雨天</router-link>
                        <router-view></router-view>
                        </div>`
                    },
                    children:[
                        {
                            path: 'rain',
                            component: {
                                template: `
                                <div>
                                    雨天下雨了
                                </div>
                                `
                            }
                        }
                    ]
                },{
                    path: '/aaa',
                    meta: {
                        mustLogin: true
                    },
                    component: {
                        template: `
                        <div>
                            这是aaaa的内容
                        </div>
                        `
                    }
                }
            ];

            const vRouter = new VueRouter({
                routes: routesArr
            })

            vRouter.beforeEach((to, from, next) => {
                
                // 用来判断用户是否登录
                let userLogin = true;

                if( !userLogin && to.matched.some(function( item ){
                    return item.meta.mustLogin;
                }) ){
                    next('/login');
                }else{
                    next();
                }
            })

            new Vue({
                el: '#div1',
                router: vRouter
            })
        }
    </script>
</body> 
</html>

 

推荐阅读