欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue-router子路由的實(shí)現(xiàn)方式

 更新時(shí)間:2023年02月06日 16:45:50   作者:楊哥學(xué)編程  
這篇文章主要介紹了vue-router子路由的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在應(yīng)用界面開(kāi)發(fā)中通常由多層嵌套的組件組合而成。

但隨著頁(yè)面的增多,如果把所有的頁(yè)面都塞到一個(gè) routes 數(shù)組里面會(huì)顯得很亂,你無(wú)法確定哪些頁(yè)面存在關(guān)系。

借助 vue-router 提供了嵌套路由的功能,讓我們能把相關(guān)聯(lián)的頁(yè)面組織在一起。

實(shí)驗(yàn)?zāi)康?/h2>

在我們的商城項(xiàng)目中,后臺(tái)管理頁(yè) Admin 涉及到很多操作頁(yè)面,比如:

  • /admin 主頁(yè)面
  • /admin/create 創(chuàng)建新信息
  • /admin/edit 編輯信息

讓我們通過(guò)嵌套路由的方式將它們組織在一起。

創(chuàng)建Admin頁(yè)面

在src/views下創(chuàng)建 Admin.vue,并創(chuàng)建admin目錄,以用來(lái)存放admin的子頁(yè)面( 使用vue-router的子路由,需要在父組件利用 router-view占位 )

Admin.vue

<template>
? <div class="title">
? ? <h1>{{ msg }}</h1>
? ? <!-- 路由插槽 -->
? ? <router-view></router-view>
? </div>
</template>

<script>
export default {
? name: "home",

? data() {
? ? return {
? ? ? msg: "This is the Admin Page",
? ? };
? },
};
</script>

<style scoped>
</style>

創(chuàng)建子頁(yè)面

在src/views下創(chuàng)建admin目錄用來(lái)存放admin的子頁(yè)面,在admin目錄下新建Create.vue 和 Edit.vue 來(lái)實(shí)現(xiàn)/create 創(chuàng)建新的商品/edit 編輯商品信息

Create.vue

<template>
? <div>
? ? <div class="title">
? ? ? <h1>This is Admin/Create</h1>
? ? </div>
? </div>
</template>

Edit.vue

<template>
? <div>
? ? <div class="title">
? ? ? <h1>This is Admin/Edit</h1>
? ? </div>
? </div>
</template>

修改router/index.js代碼

增加子路由,子路由的寫(xiě)法是在原有的路由配置下加入children字段。

children:[
? ? {path:'/',component:xxx},
? ? {path:'xx',component:xxx}
]

注意: children里面的path 不要加 / ,加了就表示是根目錄下的路由。

index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Admin from '@/views/Admin.vue'

// 導(dǎo)入admin子路由
import Create from '@/views/admin/Create';
import Edit from '@/views/admin/Edit';

Vue.use(VueRouter)

const routes = [
? {
? ? path: '/admin',
? ? name: 'Admin',
? ? component: Admin,
? ? children: [
? ? ? {
? ? ? ? path: 'create',
? ? ? ? component: Create,
? ? ? },
? ? ? {
? ? ? ? path: 'edit',
? ? ? ? component: Edit,
? ? ? }
? ? ]
? }
]

const router = new VueRouter({
? routes
})

export default router

至此 Vue-router 子路由(嵌套路由)創(chuàng)建完成

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論