【Vue-Router】命名视图

命名视图

同时 (同级) 展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar (侧导航) 和 main (主内容) 两个视图,这个时候命名视图就派上用场了。

可以在界面中拥有多个单独命名的视图,而不是只有一个单独的出口。

如果 router-view 没有设置名字,那么默认为 default。一个视图使用一个组件渲染,因此对于同个路由,多个视图就需要多个组件。

First.vue

html 复制代码
<template>
  <h1>First Seciton</h1>
</template>

Second.vue

html 复制代码
<template>
  <h1>Second Seciton</h1>
</template>

Third.vue

html 复制代码
<template>
  <h1>Third Seciton</h1>
</template>

index.ts

ts 复制代码
import { createRouter, createWebHistory } from 'vue-router'
import First from '../components/First.vue'
import Second from '../components/Second.vue'
import Third from '../components/Third.vue'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      components: {
        default: First,
        a: Second,
        b: Third,
      },
    },
    {
      path: '/other',
      components: {
        default: Third,
        a: Second,
        b: First,
      },
    },
  ],
})

App.vue

html 复制代码
<template>
  <h1>Named Views</h1>
  <ul>
    <li>
      <router-link to="/">First page</router-link>
    </li>
    <li>
      <router-link to="/other">Second page</router-link>
    </li>
  </ul>
  <router-view></router-view>
  <router-view name="a"></router-view>
  <router-view name="b"></router-view>
</template>

<script setup lang="ts">

</script>

<style scoped></style>


嵌套命名视图


First.vue

html 复制代码
<template>
  <h1>First Seciton</h1>
</template>

Second.vueThird.vue代码同理

UserSettings.vue

html 复制代码
<template>
  <h1>UserSettings</h1>
  <router-link to="/settings/children1">children1</router-link>
  <br />
  <router-link to="/settings/children2">children2</router-link>
  <br>
  <button @click="toBackPage">返回</button>
  <hr>
  <router-view></router-view>
  <router-view name="a"></router-view>
  <router-view name="b"></router-view>
</template> 

<script setup lang="ts">
import { useRouter } from 'vue-router';
const router = useRouter();
const toBackPage = () => {
  router.go(-1);
}
</script>

<style scoped></style>

index.ts

ts 复制代码
import { createRouter, createWebHistory } from 'vue-router'
import First from '../components/First.vue'
import Second from '../components/Second.vue'
import Third from '../components/Third.vue'
import UserSettings from '../components/UserSettings.vue'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/settings',
      component: UserSettings,
      children: [
        {
          path: 'children1',
          components: {
            default: First,
            a: Second,
            b: Third,
          },
        },
        {
          path: 'children2',
          components: {
            default: Third,
            a: Second,
            b: First,
          },
        },
      ]
    },

  ],
})

App.vue

html 复制代码
<template>
  <h1>Nested Named Views</h1>
  <hr>
  <router-view></router-view>
  <hr>
</template>

<script setup lang="ts">

</script>

<style scoped></style>


相关推荐
weifont3 小时前
聊一聊Electron中Chromium多进程架构
javascript·架构·electron
大得3693 小时前
electron结合vue,直接访问静态文件如何跳转访问路径
javascript·vue.js·electron
it_remember5 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
敲代码的小吉米6 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
是千千千熠啊6 小时前
vue使用Fabric和pdfjs完成合同签章及批注
前端·vue.js
da-peng-song7 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
九月TTS7 小时前
TTS-Web-Vue系列:组件逻辑分离与模块化重构
前端·vue.js·重构
低代码布道师8 小时前
第五部分:第一节 - Node.js 简介与环境:让 JavaScript 走进厨房
开发语言·javascript·node.js
满怀10158 小时前
【Vue 3全栈实战】从响应式原理到企业级架构设计
前端·javascript·vue.js·vue
梅子酱~9 小时前
Vue 学习随笔系列二十三 -- el-date-picker 组件
前端·vue.js·学习