路由

93 阅读1分钟

配置路由

{
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/ChildA',
    name: 'ChildA',
    component: () => import('../components/ChildA.vue')
  },
  {
    path: '/ChildB',
    name: 'ChildB',
    component: () => import('../components/ChildB.vue')
  }

配置页面

<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'

export default {
  name: 'HomeView',
  components: {
    HelloWorld
  }
}
</script>

<template>
  <h1>我是组件A</h1>
</template>

<script>
export default {

}
</script>

<style>

</style>
<template>
  <h1>我是组件B</h1>
</template>

<script>
export default {

}
</script>

<style>

</style>

主页面:

 <div id="app">
    <nav>
      <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link>|
      <router-link to="/ChildA">ChildA</router-link>|
      <router-link to="/ChildB">ChildB</router-link>
    </nav>
    <router-view/>
  </div>

配置二级路由

 {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue'),  
    children:[{
      path:'aboutchilda',
      name:'aboutchilda',
      component:()=> import('../views/AboutChilda.vue')
    },
    {
      path:'aboutchildb',
      name:'aboutchildb',
      component:()=> import('../views/AboutChildb.vue')
    }]
  },

页面:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <router-link to="/about/aboutchilda">aboutchilda</router-link>|
    <router-link to="/about/aboutchildb">aboutchildb</router-link>
    <router-view></router-view>
  </div>
</template>

若要一进入页面就显示,添加redirect+路径