iT邦幫忙

1

vue vouter文件筆記:嵌套路由、命名路由、命名視圖、重定向和别名

  • 分享至 

  • xImage
  •  

PS: 僅記錄自己不熟的概念。

一、嵌套路由:
根目錄path要加 / ,子路由path則不用

const routes = [
  {
    path: '/user',
    component: User,
    children: [
      {
        path: 'profile',
        component: UserProfile,
      },
    ],
  },
]

二、命名路由:
name必須唯一值,不可重複

三、命名視圖:
一個SFC檔案存在多個router-view時

<router-view class="view left-sidebar" name="LeftSidebar" />
<router-view class="view main-content" />
<router-view class="view right-sidebar" name="RightSidebar" />

路由表components,必須加上s

const routes = [
  {
    path: '/',
    components: {
      default: Home,
      LeftSidebar,
      RightSidebar,
    },
  },
],

四、重定向和别名(Redirect and Alias)

  1. 重定向
    網址輸入a,導向b
    有在路由表寫redirect,省略不寫該項目的component。除非它有子路由。
  2. 別名
    在網址url輸入不同字時,都導向同一個路由表物件。
    網址輸入a, b, c,通通導向d
const routes = [
  {
    path: '/users',
    component: UsersLayout,
    children: [
      // 为这 3 个 URL 呈现 UserList
      // - /users
      // - /users/list
      // - /people
      { path: '', component: UserList, alias: ['/people', 'list'] },
    ],
  },
]

alias是一個陣列
它可加/,表示從根目錄開始


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言