代码示例:/lesson12/01. vue-router 多视图.htmlhtml
在以前的例子中,路由的组件配置都是用component,若是改成components,就能够支持在一个页面中显示多视图。vue
在router-view中添加name属性,该视图会显示对应components中同名属性的组件。git
JavaScript:github
const headerCmp={ // 组件必须有父级标签,不能够直接写入文本
template: '<div>头部</div>'
}
const footerCmp={
template: '<div>底部</div>'
}
const footerCmp2={
template: '<div>底部</div>'
}
const newsCmp={
template: '<div>新闻</div>'
}
const userCmp={
template: '<div>用户</div>'
}
const indexCmp={
template: '<div>首页</div>'
}
// 路由表
const router = new VueRouter({
routes: [
{
path: '/', // 路由的路径
name: 'index', // 路由名称,可选属性,定义后能够用其实现跳转
components: { // 经过components属性显示多个组件
default: indexCmp, // 默认视图,对应<router-view></router-view>
header: headerCmp, // 命名视图,对应<router-view name="header"></router-view>
footer: footerCmp
},
},
{
path: '/news',
name: 'news',
components: {
default: newsCmp,
header: headerCmp,
footer: footerCmp2
}
}
]
})
let vm = new Vue({
el: '#app',
data: {
},
// 将路由添加到Vue中
router,
methods: {
fn1() {
// 经过路由名称跳转,配置params参数。
this.$router.replace({ name: 'index', params: { id: Math.random() } });
},
fn2() {
// 直接跳转路由地址,参数直接带在路径中。
this.$router.push(`/news/${Math.random()}`);
},
fn3() {
// 经过路由地址进行跳转,配置query参数。
this.$router.push({ path: '/user', query: { userId: 321 } });
},
fn4() {
console.log(this.$router)
this.$router.go(1)
},
fn5() {
this.$router.forward()
},
fn6() {
this.$router.go(-1)
},
fn7() {
this.$router.back()
},
}
})
复制代码
HTML:vue-router
<div id="app">
<router-link class="nav" to="/">首页</router-link>
<router-link class="nav" to="/news">新闻</router-link>
<!-- 多个路由容器 -->
<!-- name属性的值对应路由配置中components中的属性名 -->
<router-view name="header"></router-view>
<router-view></router-view>
<router-view name="footer"></router-view>
</div>
复制代码