vue-router 中router-view不能渲染的解決方法
最近在做一個vue的項目,其中使用了vue2.0,vue-router2.0。在使用vue-router的時候跳了一個很大的坑,router-view不能渲染,花費了好多時間終于發(fā)現(xiàn)了原因。
項目目錄結(jié)構(gòu)
其中main.js
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.vue
<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.js
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中使用了各種方法,最后查看文檔發(fā)現(xiàn)原來是routes惹的禍,每次都寫的是routers,導(dǎo)致路由根本就沒有導(dǎo)入進(jìn)去,所以在渲染的時候一直不能顯示content。如下官方文檔中的例子:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3中unplugin-auto-import自動引入示例代碼
unplugin-auto-import 這個插件是為了解決在開發(fā)中的導(dǎo)入問題,下面這篇文章主要給大家介紹了關(guān)于vue3中unplugin-auto-import自動引入的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02vue中前進(jìn)刷新、后退緩存用戶瀏覽數(shù)據(jù)和瀏覽位置的實例講解
今天小編就為大家分享一篇vue中前進(jìn)刷新、后退緩存用戶瀏覽數(shù)據(jù)和瀏覽位置的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09