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