Vue Router 之 Named Views

栏目: 编程语言 · 发布时间: 6年前

内容简介: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

Vue Router 之 Named Views

  1. 點擊 About Both
  2. Route 為 /about/both
  3. 同時顯示 AboutUsAboutYou 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-usabout-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 名稱

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

永无止境

永无止境

[美] 道格拉斯•艾德华兹 / 刘纯毅 / 中信出版社 / 2012-12-15 / 59.00元

★ 值得中国初创公司反复思考的企业传记 ★ 互联网行业必读书 ★ Google高管揭开Google的神秘面纱 ★ 探寻“G力量”重塑人类知识景观的心路历程 ★ Google走过的路,Google未来的路 ★ 编辑推荐: 它是目前被公认为全球最大的搜索引擎!它是互联网上五大最受欢迎的网站之一! 它在操作界面中提供多达30余种语言选择,在全球范围内拥有无数用户......一起来看看 《永无止境》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具