内容简介:Vue Router 預設都是綁定一個 component,若想同時綁定多個 component,則要使用 Named Views。Vue 2.6.10Vue-router 3.0.3
Vue Router 預設都是綁定一個 component,若想同時綁定多個 component,則要使用 Named Views。
Version
Vue 2.6.10
Vue-router 3.0.3
Vue CLI 3.8.4
Named View
- 點擊
About Both
- Route 為
/about/both
- 同時顯示
AboutUs
與AboutYou
component
About.vue
<template> <div> <h1>About</h1> <router-link to="/about/us">About Us</router-link> | <router-link to="/about/you">About You</router-link> | <router-link to="/about/both">About Both</router-link> <router-view name="default"></router-view> <router-view name="another"></router-view> </div> </template>
第 7 行
<router-view name="default"></router-view> <router-view name="another"></router-view>
使用 name attribute 指定 <router-view/>
的 view 名稱。
<router-view name="default"></router-view>
與 <router-view></router-view>
意義是一樣的,因此 about-us
與 about-you
component 將顯示在 default
view。
當點擊 About Both
時,我們希望 about-us
component 顯示在 default
view,而 about-you
component 顯示在 another
view。
route.js
import Vue from 'vue'; import Router from 'vue-router'; import Home from './views/Home.vue'; Vue.use(Router); export default new Router({ routes: [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: () => import(/* webpackChunkName: "about" */ './views/About.vue'), children: [ { path: 'us', name: 'us', component: () => import(/* webpackChunkName: "about-us" */ './views/about-us.vue'), }, { path: 'you', name: 'you', component: () => import(/* webpackChunkName: "about-you" */ './views/about-you.vue'), }, { path: 'both', name: 'both', components: { default: () => import(/* webpackChunkName: "about-us" */ './views/about-us.vue'), another: () => import(/* webpackChunkName: "about-you" */ './views/about-you.vue'), }, }, ], }, ] })
29 行
{ path: 'both', name: 'both', components: { default: () => import(/* webpackChunkName: "about-us" */ './views/about-us.vue'), another: () => import(/* webpackChunkName: "about-you" */ './views/about-you.vue'), }, }
若要使用 named view,則要使用 components
property,為 object,其 key 為 view 名稱。
-
default
view 顯示about-us
component -
another
view 顯示about-you
component
Conclusion
- 一般而言,一個 route 都會對應一個 component,若想一個 route 同時對應兩個以上 component,則要使用 named view
- 若
<router-view/>
沒使用name
宣告 view 名稱,等同於default
view - 若要使用 named view,要在
route.js
要使用components
property,其 key 為 view 名稱
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Creative Curve
Allen Gannett / Knopf Doubleday Publishing Group / 2018-6-12
Big data entrepreneur Allen Gannett overturns the mythology around creative genius, and reveals the science and secrets behind achieving breakout commercial success in any field. We have been s......一起来看看 《The Creative Curve》 这本书的介绍吧!