最近在作一個vue的項目,其中使用了vue2.0,vue-router2.0。在使用vue-router的時候跳了一個很大的坑,router-view不能渲染,花費了好多時間終於發現了緣由。vue
項目目錄結構vue-router
其中main.jsapp
import Vue from 'vue'; import App from './App'; import router from './router'; /* eslint-disable no-new */ new Vue({ el: '#app', router, render: h => h(App) });
app.vueflex
<template> <div id="app"> <div class="tab"> <div class="tab-item"> <router-link to="/goods">商品</router-link> </div> <div class="tab-item"> <router-link to="/ratings">評論</router-link> </div> <div class="tab-item"> <router-link to="/seller">商家</router-link> </div> </div> <div> <router-view></router-view> </div> </div> </template> <script> export default { name: 'app', components: { } }; </script> <style lang="stylus" rel="stylesheet/stylus"> .tab display: flex width: 100% height: 40px line-height: 40px .tab-item flex: 1 text-align: center & > a display: block </style>
router/index.jsspa
import Vue from 'vue'; import VueRouter from 'vue-router'; import goods from '../components/goods/goods.vue'; import ratings from '../components/ratings/ratings.vue'; import seller from '../components/seller/seller.vue'; Vue.use(VueRouter); const routes = [ { path: '/goods', component: goods }, { path: '/ratings', component: ratings }, { path: '/seller', component: seller }, { path: '*', redirect: '/goods' } ]; const router = new VueRouter({ routes: routes //注意是routes而不是routers,坑就在這裏 }); export default router;
其中在index.js中使用了各類方法,最後查看文檔發現原來是routes惹的禍,每次都寫的是routers,致使路由根本就沒有導入進去,因此在渲染的時候一直不能顯示content。以下官方文檔中的例子:eslint