Vue-router的使用和出現(xiàn)空白頁(yè),路由對(duì)象屬性詳解
Vue-router的使用和出現(xiàn)空白頁(yè)
2018.08.28 更新
vue-router:前端路由系統(tǒng)——改變視圖的同時(shí)不會(huì)向后端發(fā)出請(qǐng)求
1、 hash
2、history
2018.06.25 更新
get到一個(gè)新技能
import Vue from 'vue'
import Router from 'vue-router'
import api from '../lib/service' //接口文檔
Vue.use(Router)
const router = {
mode: 'history',
routes: [{
chunkName: 'src/pages/index',
name: 'index',
path: '/',
beforeEnter: async (to, from, next) => {
await api.login().then((res) => {
console.log(res)
next()
}).catch((rej) => {
console.log('error')
console.log(rej)
next()
})
},
component: () => import('../../src/pages/index.vue')
}]
}
export default new Router(router)
beforeEnter:在加載路由時(shí)可以做一些事情,上面的代碼執(zhí)行的是,在加載之前調(diào)用登陸接口
2018 5.5 更新
空白頁(yè)還有一種情況,頁(yè)面中數(shù)據(jù)使用的錯(cuò)誤,會(huì)導(dǎo)致空白頁(yè)
可以帶參數(shù)傳路由,有興趣的小伙伴可以試試
這個(gè)方法是我經(jīng)常用的
this.$route.push({
path: '路徑',
query: {
key: 'value'
}
})
跳轉(zhuǎn)至另一個(gè)頁(yè)面時(shí),這樣獲取傳的參數(shù)
this.$route.query.key
兩種設(shè)計(jì)模式
history/hash
還有一些別的我記錄的方法
$route.path
$route.params
$route.query
$route.hash
$route.matched //路由記錄
$route.name
$route.fullPath //包含查詢參數(shù)和hash完整路徑
route.go(num)
router-link :to=”path”
//原來寫的
自己之前跟著vue教學(xué)視頻跟著老師一起打代碼,現(xiàn)在為了配合課程設(shè)計(jì)準(zhǔn)備自己寫個(gè)vue項(xiàng)目,剛開始就在vue-router上遇到了小坎坷,就想分享一下
放上代碼
main.js
import VueResource from 'vue-resource'
import Index from './pages/index'
import Content from './pages/content'
import router from './router'
import Router from 'vue-router'
Vue.config.productionTip = false
Vue.use(Router)
Vue.use(VueResource)
let routers = new Router({
mode: 'history',
routes: [
{
path: '/',
component: Content,
children: [
{
path: '/content',
component: Content
}
]
}
]
})
/* eslint-disable no-new */
new Vue({
el: '#app',
routers,
template: '<Index/>',
components: { Index }
})
index.vue
<template>
<div id="app" class="wrapper">
<div class="nav">
<ul>
<li>首頁(yè)</li>
<li>技術(shù)文檔</li>
<li>留言</li>
<li>關(guān)于我</li>
</ul>
</div>
<div class="content">
<router-view></router-view>
</div>
<div class="footer">
@dwf
</div>
</div>
</template>
<script>
</script>
<style>
</style>
content.vue
<template> <div> 1111 </div> </template> <script> </script> <style> </style>
這樣寫下來,沒報(bào)錯(cuò),可是運(yùn)行以后就是空白頁(yè)
之前是因?yàn)樯身?xiàng)目時(shí),我就直接用了router,為了不沖突自己生成的router,我自己改了名稱routers, 后來考慮到是不是import router from './router'這個(gè)不起作用,就刪掉了,自己cnpm vue-router。但是還是沒有用。
后來把routers改了, 把這個(gè)routers改成router,頁(yè)面就出現(xiàn)了。
let routers = new Router({
當(dāng)然下面的routers也改了。
vue-router的使用流程:
cnpm install vue-router –save;
import Router from vue-router;
Vue.use(Router);
let router = new Router({
routes: [//路由路徑]
});
new Vue({ router })
使用
完畢
然后有幾點(diǎn)注意事項(xiàng),以下幾點(diǎn)都是我碰到出現(xiàn)了空白頁(yè)的情況,po出來可能會(huì)有點(diǎn)幫助:
routes:不是routers
let router = new Router({}) 不要亂起名字 //雖然我現(xiàn)在還不知道為什么,有大神可以賜教一下嘛
不要忘記掛載在new Vue({})里面
子路由的路徑前面不要加‘/'
let router = new VueRouter({
mode: 'history',
routes: [
{
path: '/',
component: IndexPage
},
{
path: '/orderList',
component: OrderListPage
},
{
path: '/detail',
component: DetailPage,
redirect: '/detail/count',
children: [
{
path: 'analysis',
component: DetailAnaPage
}
]
}
]
})
以上這篇Vue-router的使用和出現(xiàn)空白頁(yè),路由對(duì)象屬性詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue3使用vue-router及路由權(quán)限攔截方式
- Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取
- vue-router重定向和路由別名的使用講解
- 使用vue-router為每個(gè)路由配置各自的title
- 使用vue-router beforEach實(shí)現(xiàn)判斷用戶登錄跳轉(zhuǎn)路由篩選功能
- 使用vue-route 的 beforeEach 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗(yàn)證登錄)功能
- vue-router:嵌套路由的使用方法
- VueJs路由跳轉(zhuǎn)——vue-router的使用詳解
- 路由vue-route的使用示例教程
相關(guān)文章
vue開發(fā)移動(dòng)端使用better-scroll時(shí)click事件失效的解決方案
這篇文章主要介紹了vue開發(fā)移動(dòng)端使用better-scroll時(shí)click事件失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
一文帶你了解什么是Vue的前端微服務(wù)架構(gòu)(Micro Frontends)
微前端架構(gòu)是一種將大型前端應(yīng)用拆分為多個(gè)小型、獨(dú)立的前端應(yīng)用的架構(gòu)風(fēng)格,每個(gè)小型前端應(yīng)用都可以獨(dú)立部署、獨(dú)立開發(fā)和獨(dú)立運(yùn)行,下面我們就來學(xué)習(xí)一下它的相關(guān)使用吧2023-11-11
vue+element搭建后臺(tái)小總結(jié) el-dropdown下拉功能
這篇文章主要為大家詳細(xì)介紹了vue+element搭建后臺(tái)小總結(jié),el-dropdown下拉功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
解決axios:"timeout of 5000ms exceeded"
這篇文章主要介紹了解決axios:"timeout of 5000ms exceeded"超時(shí)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue elementUi+sortable.js實(shí)現(xiàn)嵌套表格拖拽問題
這篇文章主要介紹了vue elementUi+sortable.js實(shí)現(xiàn)嵌套表格拖拽問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
vue3項(xiàng)目中配置sass,vite報(bào)錯(cuò)Undefined mixin問題
這篇文章主要介紹了vue3項(xiàng)目中配置sass,vite報(bào)錯(cuò)Undefined mixin問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例
防抖節(jié)流是可以說是一種優(yōu)化組件性能的技巧,可以有效減少組件中的渲染次數(shù)和計(jì)算量,本文主要介紹了Vue3項(xiàng)目中使用防抖節(jié)流的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-04-04
vuejs實(shí)現(xiàn)標(biāo)簽選項(xiàng)卡動(dòng)態(tài)更改css樣式的方法
這篇文章主要介紹了vuejs實(shí)現(xiàn)標(biāo)簽選項(xiàng)卡-動(dòng)態(tài)更改css樣式的方法,代碼分為html和js兩部分,需要的朋友可以參考下2018-05-05

