Vue-router-02

傳參和動(dòng)態(tài)路由

使用query傳參

<router-link to="/ChildB?name=admin">ChildB-admin</router-link>?

<router-link to="/ChildB?name=zhangsan">ChildB-zhangsan</router-link>

在ChildB組件中用watch監(jiān)聽傳參$route.query,msg來(lái)表示相應(yīng)傳參對(duì)應(yīng)的值

watch: {

? ? ? /* 通過(guò)計(jì)算屬性也可以實(shí)現(xiàn),計(jì)算屬性具有緩存功能

? ? ? ? ?全局的路由屬性$route發(fā)生了變化,也會(huì)觸發(fā)計(jì)算屬性*/

? ? $route: {

? ? ? handler() {

? ? ? ? if (this.$route.query.name == "admin") {

? ? ? ? ? this.msg = "歡迎admin管理員";

? ? ? ? } else if (this.$route.query.name == "zhangsan") {

? ? ? ? ? this.msg = "歡迎zhansgan貴賓一位,請(qǐng)上三樓";

? ? ? ? }else{

? ? ? ? ? ? this.msg = "您還不是會(huì)員,請(qǐng)充值";

? ? ? ? }

? ? ? },

? ? ? /* 進(jìn)入ChildB組件就執(zhí)行 */

? ? ? immediate:true

? ? },

? }

動(dòng)態(tài)路由傳參

<router-link to="/ChildA/1">ChildA--1</router-link>?

<router-link to="/ChildA/2">ChildA--2</router-link>

第一種方式

main,js文件中對(duì)應(yīng) path: '/childA/:id'

在ChildA組件中用watch監(jiān)聽傳參$route.params,msg來(lái)表示相應(yīng)傳參對(duì)應(yīng)的值

watch:{

? ? ? ? $route:{

? ? ? ? ? ? handler(){

? ? ? ? ? ? ? ? if(this.$route.params.id==1){

? ? ? ? ? ? ? ? ? ? this.msg='我是id1'

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if(this.$route.params.id==2){

? ? ? ? ? ? ? ? ? ? this.msg='我是id2'

? ? ? ? ? ? ? ? }

? ? ? ? ? ? },

? ? ? ? ? ? immediate:true? ? ?/* 一進(jìn)入組件就執(zhí)行 */

? ? ? ? }

? ? }

也可以使用計(jì)算屬性的寫法

computed: {

? ? str() {

let id = this.$route.params.id;

? ? ? if (id == 1) {

? ? ? ? return "我是id1";

? ? ? } else if (id == 2) {

? ? ? ? return "我是id2";

? ? ? } else {

? ? ? ? return "";

? ? ? }?

第二種方式

main,js文件中對(duì)應(yīng) path: '/childA/:id',props: true,

在childA里可以使用props接收傳參

props:['id'],

if (this.id == 1) {

? ? ? ? return "我是id1";

? ? ? } else if (this.id == 2) {

? ? ? ? return "我是id2";

? ? ? } else {

? ? ? ? return "";

? ? ? }


添加路由

addR(){

? ? ? this.$router.addRoute({

? ? ? ? path:'/vip',

? ? ? ? name:"vip",

? ? ? ? component:()=>import('@/views/VipView.vue')

? ? ? })

? ? }

添加子路由

addA(){

? ? ? /* 這里的about是name不是path */

? ? ? this.$router.addRoute('about',{

? ? ? ? path:'AboutChildC',

? ? ? ? name:'AboutChildC',

? ? ? ? component:()=>import('@/views/AboutChildC.vue')

? ? ? })

? ? }

?$route 和 $router的區(qū)別

? ? ? ?$route可以獲取路由的屬性 比如query傳參 動(dòng)態(tài)路由

? ? ? ?$router提供了一些方法,push跳轉(zhuǎn)頁(yè)面,addRoute增加路由 包括一些路由信息,比如當(dāng)前所在路由$router.currentRoute

頁(yè)面跳轉(zhuǎn)

goAbout(){

? ? ? /* 只跳轉(zhuǎn)一次,多次會(huì)報(bào)錯(cuò) ?在router中可以解決 */

? ? ? /* 第一種方式:? this.$router.push('/about') */

? ? ? ? ?第二種方式:? this.$router.push({name:'about'})

? ? }

解決多次報(bào)錯(cuò)的方法是在main.js文件中加上如下片段:本質(zhì)上就是不報(bào)錯(cuò),捕獲錯(cuò)誤信息

const VueRouterPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(to) {

? return VueRouterPush.call(this, to).catch(err => err)

}

全局路由鉤子函數(shù)

全局路由守衛(wèi)?

router.beforeEach((to, from, next) => {

? /* to表示要去的路由

? from表示之前的路由

? next表示要去執(zhí)行的行數(shù) */

? /* console.log('to',to);

? console.log('from',from);

? console.log('next',next); */

? next()

})

監(jiān)聽全局路由離開時(shí)觸發(fā)的鉤子函數(shù),★沒(méi)有next()?

router.afterEach((to,from)=>{

? console.log(to);

? console.log(from);

})?

局部路由鉤子

{

? ? path: '/childB',

? ? name: 'childB',

? ? component: () => import(/* webpackChunkName: "about" */ '../components/ChildB.vue'),

? ? /* 局部的路由鉤子函數(shù)進(jìn)入路由的時(shí)候觸發(fā) */

因?yàn)閰?shù)或者是動(dòng)態(tài)路由,在同一個(gè)路由下不會(huì)觸發(fā)beforeEnter?

? ? beforeEnter: (to, from, next) => {

? ? ? /* console.log('to',to);

? ? ? ? ?console.log('from',from);

? ? ? ? ?console.log('next',next); */

? ? ? next()

? ? }

? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 生命周期 // 創(chuàng)建之前,在beforeCreate生命周期執(zhí)行的時(shí)候,data和methods中的數(shù)據(jù)都還沒(méi)有初...
    人在胖天在看_640c閱讀 2,301評(píng)論 1 28
  • 首先拋出這樣一個(gè)問(wèn)題,vue-router是用來(lái)做什么的? 這里不著急回答,也不準(zhǔn)備在這篇文章里回答。這篇文章僅總...
    1263536889閱讀 650評(píng)論 0 2
  • 1.vue中全局注冊(cè)組件:https://blog.csdn.net/qq_34089503/article/de...
  • vue 2.0 漸進(jìn)式框架 MVC 單向通信 > m:model 數(shù)據(jù)層 保存數(shù)據(jù) > v:view視圖層 用戶界...
    web前端ling閱讀 756評(píng)論 0 0
  • Vue Vue是一個(gè)前端js框架,由尤雨溪開發(fā),是個(gè)人項(xiàng)目 Vue近幾年來(lái)特別的受關(guān)注,三年前的時(shí)候angular...
    hcySam閱讀 301評(píng)論 0 0