vue-cli 路由、子路由

打开命令行下载
cnpm i vue-router -S
在components里创建one,two文件
one
<template>
    <div>one component</div>
</template>
two
<template>
    <div>two component</div>
</template>
第一种,在src里main.js里写上
import Router from 'vue-router'

import one from './components/one'
import two from './components/two'

Vue.use(Router);

const router = new Router({
    routes:[
        {
            path:'/',
            component:one  // 跳转到one
        },
        {
            path:'/two',
            component:two // 跳转到two
        }
    ]
})


new Vue({
  el: '#app',
  router,         // 加上router
  template: '<App/>',
  components: { App }
})
第二种,在src里创建一个 router 文件夹 里面创建一个加上文件,必须叫 index.js
import Vue from 'vue'
import Router from 'vue-router'
import one from '../components/one'
import two from '../components/two'

Vue.use(Router);
export default new Router({
    routes:[
        {
            path:'/',
            component:one
        },
        {
            path:'/two',
            component:two
            ....
        {
            path:'*',  
            redirect:'/'   // 在网址输入任何数都跳到 one 页面
        }
    ]
})

子路由

// 再创两个文件 twoa.vue 和 twob.vue
        ......
                  {
            path:'/two',
            component:two,
            children:[
                {
                    path:'a',
                    component:twoa
                },
                {
                    path:'b',
                    component:twob
                }
            ]
        },
        ......
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容