欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue?編程式路由導(dǎo)航的實(shí)現(xiàn)示例

 更新時(shí)間:2022年04月18日 08:28:36   作者:Errol_King  
本文主要介紹了Vue?編程式路由導(dǎo)航

router- link的replace屬性

1.作用:控制路由跳轉(zhuǎn)時(shí)操作瀏覽器歷史記錄的模式
2.瀏覽器的歷史記錄有兩種寫入方式:分別為push和replace,push是追加歷史記錄,replace是替換當(dāng)前記錄。路由跳轉(zhuǎn)時(shí)候默認(rèn)為push
3.如何開啟replace模式News

編程式路由導(dǎo)航

作用:不借助<router-link> 實(shí)現(xiàn)路由跳轉(zhuǎn),讓路由跳轉(zhuǎn)更加靈活
具體編碼:

/ /$router的兩個(gè)API
this.$router.push({
	name : 'xiangqing',
	params :{
		id :xxx,
		title :xXX
	}
})
this.$router.replace({
	name : "xiangqing',
	params :{
		id : xxX,
		title :xXX
	}
})
this.$router.forward()//前進(jìn)
this.$router.back()//后退
this.$router.go()//可前進(jìn)也可后退

在這里插入圖片描述

修改 Banner

<template>
  <div class="col-xs-offset-2 col-xs-8">
    <div class="page-header">
      <h2>Vue Router Demo</h2>
      <button @click="back">后退</button>
      <button @click="forward">前進(jìn)</button>
      <button @click="test">測(cè)試一下 go</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "Banner",
  methods:{
    back(){
      this.$router.back()
    },
    forward(){
      this.$router.forward()
    },
    test(){
      this.$router.go(-3)
    }
  }
}
</script>

修改 Message

                <!--跳轉(zhuǎn)路由并攜帶params參數(shù),to的對(duì)象寫法-->
        ......
        <button @click="pushShow(m)">push查看</button>
        <button @click="replaceShow(m)">replace查看</button>

<script>
export default {
  ......
  methods: {
    pushShow(m) {
      this.$router.push({
        name: 'xiangqing',
        query: {
          id: m.id,
          title: m.title
        }
      })
    },
    replaceShow(m) {
      this.$router.replace({
        name: 'xiangqing',
        query: {
          id: m.id,
          title: m.title
        }
      })
    }
  }
}
</script>

緩存路由組件

作用:讓不展示的路由組件保持掛載,不被銷毀
具體編碼:

<keep-alive include="News">
	<router-view></router-view>
</keep-alive>

修改 News 組件,每個(gè)消息后展示輸入框

<template>
  <ul>
    <li>news001 <input/></li>
    <li>news002 <input/></li>
    <li>news003 <input/></li>
  </ul>
</template>

當(dāng)我們?cè)诤筮呡斎肟蛑休斎胛淖?,切換到別的鏈接,再切回來,會(huì)發(fā)現(xiàn) input 內(nèi)的文字就清空了,因?yàn)槲覀冎罢f過,當(dāng) News 切換走的時(shí)候,它已經(jīng)被銷毀了,我們可以在 beforeDestroy 中打印測(cè)試,這里不做演示

在這里插入圖片描述

想要緩存這個(gè)頁面,可以在 News 的最終展示區(qū) Home 中增加標(biāo)簽router-view,然后使用 include 標(biāo)識(shí)要緩存的組件名

<keep-alive include="News"><router-view></router-view></keep-alive>

再看效果

在這里插入圖片描述

注意:
1、include后是組件名稱
2、不寫會(huì)緩存所有的
3、如果想緩存多個(gè),可以寫成數(shù)組

:include="['News','Message']"

兩個(gè)新的生命鉤子

作用:路由組件所獨(dú)有的兩個(gè)鉤子,用于捕獲路由組件的激活狀態(tài)
activated路由組件被激活時(shí)觸發(fā)
deactivated路由組件失活時(shí)觸發(fā)

現(xiàn)在的需求是,我們希望在 News 組件周而復(fù)始的展示一個(gè)文字,之前我們寫過就是使用 setInterval,然后在 beforeDestroy 中再銷毀這個(gè) setInterval,但是由于我們上一節(jié)中,已經(jīng)把 News 設(shè)置了緩存,不會(huì)走 beforeDestroy 了,所以我們引入兩個(gè)新的生命周期鉤子

修改 News

<template>
  <ul>
    <li :style="{opacity}">歡迎學(xué)習(xí)Vue</li>
    <li>news001 <input/></li>
    <li>news002 <input/></li>
    <li>news003 <input/></li>
  </ul>
</template>

<script>
export default {
  name: "News",
  data() {
    return {
      opacity: 1
    }
  },
  activated() {
    this.timer = setInterval(() => {
      this.opacity -= 0.01
      if (this.opacity <= 0) this.opacity = 1
    },16)
  },
  deactivated() {
    clearInterval(this.timer);
  }
}
</script>

在這里插入圖片描述

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

相關(guān)文章

最新評(píng)論