2018年11月11日 關於Vue的命名檢視 and 導航鉤子 and 元資料及路由匹配
阿新 • • 發佈:2018-11-12
1、命名檢視
//在html中相關程式碼 <div id="app"> <div> <router-link to="/">首頁</router-link> <router-link to="/user">使用者管理</router-link> <router-link to="/post">帖子管理</router-link> </div> <div> <router-view></router-view> <router-view name="sidebar"></router-view> <router-view name="content"></router-view> </div> </div>
//在Vue.js中相關程式碼 var nqy = [ { path:'/', component:{ template:` <div> <h1>首頁</h1> </div> `, } } , { path:'/user', components:{ sidebar:{ template:` <div> <ul> <li>使用者列表</li> <li>使用者許可權</li> </ul> </div> `, }, content:{ template:` <div>how are you ? i am fine ,thank you , and you?how are you ? i am fine ,thank you , and you?</div> `, } } } , { path:'/post', components:{ sidebar:{ template:` <div> <ul> <li>帖子列表</li> <li>帖子許可權</li> </ul> </div> `, }, content:{ template:` <div>how are you ? i am fine ,thank you , and you?how are you ? i am fine ,thank you , and you?</div> `, } } } , ]; var nyp = new VueRouter({ routes:nqy, }); var app = new Vue({ el:'#app', router:nyp, methods:{ surf:function () { setTimeout(function () { this.nyp.push('/about'); setTimeout(function () { this.nyp.push({name:'user',params:{name:'張三三'}}) },2000) },2000) } } });
2、導航鉤子
//在html中相關程式碼 <div> <router-link to="/">首頁</router-link> <router-link to="/login">登入</router-link> <router-link to="/post">帖子管理</router-link> </div> <div> <router-view></router-view> </div> </div>
在Vue.js中相關程式碼
var nqy = [
{
path:'/',
component:{
template:`
<div>
<h1>首頁</h1>
</div>
`,
}
} ,
{
path:'/login',
component:{
template:`
<div>
<h1>登入</h1>
</div>
`,
}
},
{
path:'/post',
component:{
template:`
<div>
<h1>帖子管理</h1>
</div>
`,
}
}
];
var nyp = new VueRouter({
routes:nqy,
});
nyp.beforeEach(function (to,from,next) {
var logged_in = false;
if (!logged_in && to.path =='/post')
next('/login');
else
next();
});
var app = new Vue({
el:'#app',
router:nyp,
});
3、元資料及路由匹配
//在html中的相關程式碼
<div id="app">
<div>
<router-link to="/">首頁</router-link>
<router-link to="/login">登入</router-link>
<router-link to="/post">帖子管理</router-link>
<router-link to="/a">a</router-link>
</div>
<div>
<router-view></router-view>
</div>
</div>
//在Vue.js中的相關程式碼
var nqy = [
{
path:'/',
component:{
template:`
<div>
<h1>首頁</h1>
</div>
`,
}
} ,
{
path:'/a',
meta:{
login_required:true,
},
component:{
template:`
<div>
<h1>A</h1>
</div>
`,
}
} ,
{
path:'/login',
component:{
template:`
<div>
<h1>登入</h1>
</div>
`,
}
},
{
path:'/post',
meta:{
login_required:true,
},
component:{
template:`
<div>
<h1>帖子管理</h1>
<div>
<router-link to="more" append>帖子內容</router-link>
<router-view></router-view>
</div>
</div>
`,
},
children:[
{
path:'more',
component:{
template:`
<div>how are you ? i am fine ,thank you , and you?how are you ? i am fine ,thank you , and you?</div>
`,
}
}
]
}
];
var nyp = new VueRouter({
routes:nqy,
});
nyp.beforeEach(function (to,from,next) {
var logged_in = false;
if (!logged_in && to.matched.some(function (item) {
return item.meta.login_required;
}))
next('/login');
else
next();
});
var app = new Vue({
el:'#app',
router:nyp,
});