1. 安装 vue-router
    1. npm install --save vue-router
    1. ,main.js中添加以下代码
    1. import Vue from 'vue';
    2. import VueRouter from 'vue-router';
    3. import App from './App.vue';
    4. Vue.use(VueRouter);
    5. Vue.config.productionTip = false
    6. const Routers = [
    7. {
    8. path:'/index',
    9. component:(resolve) => require(['./components/index.vue'],resolve)
    10. },
    11. {
    12. path:'/about',
    13. component:(resolve) => require(['./components/about.vue'],resolve)
    14. },
    15. {
    16. path:'*',
    17. redirect:'/index'
    18. }
    19. ]
    20. const RouterConfig = {
    21. mode:'history',
    22. routes:Routers
    23. }
    24. const router = new VueRouter(RouterConfig)
    25. new Vue({
    26. el:'#app',
    27. router:router,
    28. render: h => h(App),
    29. }).$mount('#app')
    1. 分别创建2个component文件
    1. //about.vue
    2. <template>
    3. <div>123a</div>
    4. </template>
    5. <script>
    6. export default {
    7. }
    8. </script>
    9. //index.vue
    10. <template>
    11. <div>11322</div>
    12. </template>
    13. <script>
    14. export default {
    15. }
    16. </script>
    1. app.vue中添加路由占位符
    1. <template>
    2. <div id="app">
    3. <router-view></router-view>
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. }
    9. </script>