vue路由钩子

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0" />
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.bootcss.com/vue-router/2.7.0/vue-router.min.js"></script>
</head>
<body>
<!--https://segmentfault.com/a/1190000012552286-->
<div id="app">
<p>
<ul>
<li @click="gouZhi"><router-link to="/qz">意向</router-link></li>
<li @click="" ><router-link to="/jn">特长</router-link></li>
<li @click=""><router-link to="/gz">经验</router-link></li>
<li><router-link to="/zw">评价</router-link></li>
<li> <router-link to="/jy">背景</router-link></li>
</ul>
</p>
<router-view></router-view>
<button>点击获取参数</button>
</div>

<template id="yiXiang">
<div>求职意向</div>
</template>
<template id="jiNeng">
<div>技能特长</div>
</template>
<template id="gongZuo">
<div>
<ul>
<li><router-link to="/gz/qzSon/234">云贷通</router-link></li>
<li><router-link to="/gz/jnSon">有数简查</router-link></li>
<li><router-link to="/gz/gzSon">最多跑一次</router-link></li>
<li><router-link to="/gz/zwSon">接口微服务</router-link></li>
</ul>
<router-view></router-view>
</div>
</template>
<template id="ziWo">
<div>自我评价</div>
</template>
<template id="jiaoYu">
<div>教育背景</div>
</template>

<!--路由子模块-->
<template id="gongZuo1">
<div>《2323》</div>
</template>
<template id="gongZuo2">
<div>《2434》</div>
</template>
<template id="gongZuo3">
<div>《45454》</div>
</template>
<template id="gongZuo4">
<div>《6767676》</div>
</template>
<script>
const yiXiang = { template: '#yiXiang'};
const jiNeng = { template: '#jiNeng'};
const gongZuo = { template: '#gongZuo'};
const ziWo = { template: '#ziWo'};
const jiaoYu = { template: '#jiaoYu'};

const jiaoYuSon1 = { template: '#gongZuo1'};
const jiaoYuSon2 = { template: '#gongZuo2'};
const jiaoYuSon3 = { template: '#gongZuo3'};
const jiaoYuSon4 = { template: '#gongZuo4'};

const router = new VueRouter({
routes:[
{path:'',component:yiXiang},
{
path: '/qz/',
component: yiXiang,
beforeEnter: (to, from, next) => {console.log(1)},
beforeLeave: (to, from, next) => {},

},
{ path: '/jn/', component: jiNeng,name:'jiNeng2'},
{ path: '/gz', component: gongZuo,name:'gongZuo',
children: [
{ path: "/", component: jiaoYuSon1 },
{ path: "qzSon", component: jiaoYuSon1 },
{ path: "jnSon", component: jiaoYuSon2 },
{ path: "gzSon", component: jiaoYuSon3 },
{ path: "zwSon", component: jiaoYuSon4 }
]
},
{ path: '/zw', component: ziWo},
{ path: '/jy', component: jiaoYu}
]
});
router.beforeEach((to, from, next) => { //全局路由用于判断用户登录挺好的
console.log(1);
next();//可带路径参数跳转到指定路径
})
const app = new Vue({
methods:{
getFn:function () {},
},
router
}).$mount('#app');

//1 全局钩子
//2 某个路由钩子 写在路由配置上
//3 某个组件钩子 和method平级写法
</script>
</body>
</html>
posted @ 2018-04-21 00:54  zou1234  阅读(451)  评论(0编辑  收藏  举报