(1) 安装插件

npm install —save vue-router

image.png

(2) routes.js

image.png

  1. import ShowBlog from './components/ShowBlog.vue'
  2. import AddBlog from './components/AddBlog.vue'
  3. export default[
  4. {path: "/", component:ShowBlog},
  5. {path: "/add", component:AddBlog},
  6. ]

(3) main.js

  1. import VueRouter from 'vue-router'
  2. import Routes from './routes'
  3. Vue.use(VueRouter)
  4. // 自定义路由
  5. const router = new VueRouter({
  6. routes: Routes,
  7. mode: "history" // 加了这个路由后就不会有/#
  8. })
  9. /* eslint-disable no-new */
  10. new Vue({
  11. el: '#app',
  12. components: { App },
  13. template: '<App/>',
  14. router: router,
  15. })

(4) App.vue

  1. <template>
  2. <div id="app">
  3. <!-- <add-blog></add-blog> -->
  4. <!-- <show-blog></show-blog> -->
  5. <router-view></router-view>
  6. </div>
  7. </template>