命名視圖
同時(shí) (同級(jí)) 展示多個(gè)視圖,而不是嵌套展示,例如創(chuàng)建一個(gè)布局,有 sidebar
(側(cè)導(dǎo)航) 和 main
(主內(nèi)容) 兩個(gè)視圖,這個(gè)時(shí)候命名視圖就派上用場了。
可以在界面中擁有多個(gè)單獨(dú)命名的視圖,而不是只有一個(gè)單獨(dú)的出口。
如果 router-view 沒有設(shè)置名字,那么默認(rèn)為 default。一個(gè)視圖使用一個(gè)組件渲染,因此對于同個(gè)路由,多個(gè)視圖就需要多個(gè)組件。
First.vue
<template>
<h1>First Seciton</h1>
</template>
Second.vue
<template>
<h1>Second Seciton</h1>
</template>
Third.vue
<template>
<h1>Third Seciton</h1>
</template>
index.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
<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
<template>
<h1>First Seciton</h1>
</template>
Second.vue,Third.vue代碼同理
UserSettings.vue
<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
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文章來源:http://www.zghlxwxcb.cn/news/detail-649107.html
<template>
<h1>Nested Named Views</h1>
<hr>
<router-view></router-view>
<hr>
</template>
<script setup lang="ts">
</script>
<style scoped></style>
文章來源地址http://www.zghlxwxcb.cn/news/detail-649107.html
到了這里,關(guān)于【Vue-Router】命名視圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!