亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue聲明式導(dǎo)航與編程式導(dǎo)航示例分析講解

 更新時(shí)間:2022年11月30日 10:43:19   作者:未及545  
這篇文章主要介紹了Vue中聲明式導(dǎo)航與編程式導(dǎo)航,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧

聲明式導(dǎo)航:在瀏覽器中,點(diǎn)擊鏈接實(shí)現(xiàn)導(dǎo)航的方式,叫做聲明式導(dǎo)航。如:普通網(wǎng)頁(yè)中點(diǎn)擊<a>鏈接,vue中點(diǎn)擊<router--link>都屬于聲明式導(dǎo)航。

編程式導(dǎo)航:在瀏覽器中,調(diào)用API方法實(shí)現(xiàn)導(dǎo)航的方式,叫做編程式導(dǎo)航。如:普通網(wǎng)頁(yè)中調(diào)用location.href跳轉(zhuǎn)到新頁(yè)面的方式,屬于編程式導(dǎo)航。

vue-router中的編程式導(dǎo)航

vue-router提供了許多編程式導(dǎo)航的API,其中最常用的三種API分別是:

1.this.$router.push("hash地址")

跳轉(zhuǎn)到指定hash地址,并增加一條歷史記錄。

2.this.$router.replace("hash地址")

跳轉(zhuǎn)到指定的hash地址,并替換掉當(dāng)前的歷史記錄。

3.this.$router.go(數(shù)值n)

在瀏覽歷史前進(jìn)或后退,()中的值為整數(shù),負(fù)值代表后退,正值代表前進(jìn)。

在實(shí)際開(kāi)發(fā)中,一般只會(huì)前進(jìn)或后退一層頁(yè)面,因此可用簡(jiǎn)化用法:

①$router.back()

在歷史記錄中,后退到上一個(gè)頁(yè)面。

②$router.forward()

在歷史記錄中,前進(jìn)到下一個(gè)頁(yè)面。

<template lang="">
  <div>
    music組件
    <!-- <p>{{this.$route.params.id}}</p> -->
    <p>{{id}}</p>
    <button @click="btn">點(diǎn)擊打印this</button>
    <button @click="goTo">跳轉(zhuǎn)到金玉良緣</button>
    <button @click="$router.back()">后退</button>
    <button @click="$router.forward()">前進(jìn)</button>
  </div>
</template>
<script>
export default{
  props:["id"],
  methods:{
    btn(){
      console.log(this);
    },
    goTO(){
      this.$router.replace("/music4")
    }
  }
}
</script>
<style lang="less" scoped>
div{
width: 100%;
height: 50px;
background-color:orangered;
}
</style>

導(dǎo)航守衛(wèi)

導(dǎo)航守衛(wèi)可以控制路由的訪問(wèn)權(quán)限。

全局前置守衛(wèi)

每次發(fā)生路由的導(dǎo)航跳轉(zhuǎn)時(shí),都會(huì)觸發(fā)全局前置守衛(wèi)。因此,在全局前置守衛(wèi)中,我們可以對(duì)每個(gè)路由進(jìn)行訪問(wèn)權(quán)限的控制。

next的三種調(diào)用方式:

1.當(dāng)前用戶(hù)擁有后臺(tái)主頁(yè)的訪問(wèn)權(quán)限,直接放行:next()

2.當(dāng)前用戶(hù)沒(méi)有后臺(tái)主頁(yè)的訪問(wèn)權(quán)限,強(qiáng)制其跳轉(zhuǎn)到登錄頁(yè)面:next("/login")

3.當(dāng)前用戶(hù)沒(méi)有后臺(tái)主頁(yè)的訪問(wèn)權(quán)限,不允許跳到后臺(tái)主頁(yè):next(false)

import Vue from "vue";
import VueRouter from "vue-router";
import child from "@/components/child.vue"
import left from "@/components/left.vue"
import right from "@/components/right.vue"
import Tab1 from "@/components/tabs/Tab1.vue"
import Tab2 from "@/components/tabs/Tab2.vue"
import music from "@/components/music.vue"
import login from "@/components/login.vue"
import background from "@/components/background.vue"
Vue.use(VueRouter)
const router=new VueRouter({
  routes:[
    {path:"/",redirect:"/firstPage"},
    {path:"/music:id",component:music,props:true},
    {path:"/firstPage",component:child,redirect:"/firstPage/tab1",children:[
 {path:"tab1",component:Tab1},{path:"tab2",component:Tab2}]},
    {path:"/img",component:left},
    {path:"/video",component:right},
    {path:"/login",component:login},
    {path:"/background",component:background}
  ]
})
router.beforeEach(function(to,from,next){
  // 要拿到用戶(hù)將要訪問(wèn)的hash地址
  // 判斷hash地址是否等于/background
  // 如果等于,證明需要登錄之后,才能訪問(wèn)成功
  // 如果不等于,則不需要登錄,直接放行
  // 如果訪問(wèn)的地址是/background,則需要讀取localStorage中的token值
  // 如果有token,則放行,如果沒(méi)有,則強(qiáng)制跳轉(zhuǎn)到/login登錄頁(yè)
  if(to.path==="/background"){
    const token=localStorage.getItem("token")
    if(token){
      next()
    }else{
      next("/login")
    }
  }else{
    next()
  }
})
// 4.向外共享路由的實(shí)例對(duì)象
export default router

到此這篇關(guān)于Vue聲明式導(dǎo)航與編程式導(dǎo)航示例分析講解的文章就介紹到這了,更多相關(guān)Vue聲明式導(dǎo)航與編程式導(dǎo)航內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論