最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue路由監(jiān)聽(tīng)的一些常用方式

 更新時(shí)間:2023年10月09日 09:13:05   作者:前端-文龍剛  
有時(shí)在頁(yè)面刷新或者返回等操作時(shí),想監(jiān)聽(tīng)路由變化進(jìn)行數(shù)據(jù)更新等操作,下面這篇文章主要給大家介紹了關(guān)于vue路由監(jiān)聽(tīng)的一些常用方式,需要的朋友可以參考下

場(chǎng)景:

我們?cè)谧鲰?xiàng)目時(shí),會(huì)遇到部分需求是根絕路由的切換來(lái)判斷某些值,下面介紹部分常用的監(jiān)聽(tīng)方式

一、 通過(guò) watch 進(jìn)行監(jiān)聽(tīng)

// 方式1、監(jiān)聽(tīng)路由 $route 變化  一般監(jiān)聽(tīng)
export default{
watch: {
$route(to, from){
console.log('路由變化了')
console.log('當(dāng)前頁(yè)面路由:' + to.path);
console.log('上一個(gè)路由:' + from);
},
}
}
// 方式2、監(jiān)聽(tīng)路由 $route 變化, 使用handler函數(shù) 深度監(jiān)聽(tīng)路由
export default{
watch: {
'$route': { // $route可以用引號(hào),也可以不用引號(hào)  監(jiān)聽(tīng)的對(duì)象
handler(to, from){
console.log('路由變化了')
console.log('當(dāng)前頁(yè)面路由:' + to);
console.log('上一個(gè)路由:' + from);
},
deep: true, // 深度觀察監(jiān)聽(tīng) 設(shè)置為 true
immediate: true, // 第一次初始化渲染就可以監(jiān)聽(tīng)到
}
}
}
// 方式3、監(jiān)聽(tīng)路由 $route 變化,觸發(fā)methods里的方法
export default{
watch: {
'$route': 'initData'
},
methods: {
initData(){
console.log('路由變化了')
}
}
}
// 方式4、監(jiān)聽(tīng)路由的 path 變化
export default{
watch: {
'$route.path'(toPath, fromPath){
console.log('路由變化了')
console.log('當(dāng)前頁(yè)面路由地址:' + toPath)
console.log('上一個(gè)路由地址:' + fromPath)
},
}
}
// 方式5、監(jiān)聽(tīng)路由的 path 變化, 使用handler函數(shù)
export default{
watch: {
'$route.path': {
handler(toPath, fromPath){
console.log('路由變化了')
console.log('當(dāng)前頁(yè)面路由地址:' + toPath)
console.log('上一個(gè)路由地址:' + fromPath)
},
deep: true, // 深度監(jiān)聽(tīng)
immediate: true, // 第一次初始化渲染就可以監(jiān)聽(tīng)到
}
}
}
// 方式6、監(jiān)聽(tīng)路由的 path 變化,觸發(fā)methods里的方法
export default{
watch: {
'$route.path': 'initData'
},
methods: {
initData(){
console.log('路由變化了')
}
}
}

二、通過(guò)鉤子函數(shù)beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave進(jìn)行監(jiān)聽(tīng)

export default{
beforeRouteEnter(to, from, next){
// 渲染該組件前調(diào)用這個(gè)鉤子,因此組件還未被創(chuàng)建,不能獲取this
console.log(this) // 結(jié)果為:undefined
console.log('beforeRouteEnter')
next()
},
beforeRouteUpdate(to, from, next){
//這個(gè)組件是被復(fù)用的時(shí)候調(diào)用,比如帶有動(dòng)態(tài)參數(shù)的路由跳轉(zhuǎn):/add/11 跳轉(zhuǎn)到 /detail/12
console.log(this) // 可以訪問(wèn)this
console.log('beforeRouteUpdate')
next()
},
beforeRouteLeave(to, from, next){
// 導(dǎo)航離開(kāi)當(dāng)前路由的時(shí)候被調(diào)用,this可以被訪問(wèn)到
console.log(this) // 可以訪問(wèn)this
console.log('beforeRouteLeave')
next()
},
}

三、全局路由監(jiān)聽(tīng) this.$router.beforeEach

// 方式1、在App.vue的create中進(jìn)行全局路由監(jiān)聽(tīng)
export default  {
name:  'App',
created() {
this.$router.beforeEach((to, from, next) => {
console.log(to);
console.log(from);
next()
})
}
}
// 方式2、在路由文件(/router/index.js)中進(jìn)行全局路由監(jiān)聽(tīng)
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
let routes = [
{
path: '/login',
component: resolve => require(['@/views/login'], resolve),
},
]
let router = new Router({
mode: 'history', // 去掉 url 中的 #
scrollBehavior: () => ({ y: 0 }),
base: process.env.VUE_APP_BASE_DOMAIN,
routes,
})
router.beforeEach((to, from, next) => {
console.log(to);
console.log(from);
next()
})
export {
routes
router
}

總結(jié) 

到此這篇關(guān)于vue路由監(jiān)聽(tīng)的一些常用方式的文章就介紹到這了,更多相關(guān)vue路由監(jiān)聽(tīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

淮安市| 濉溪县| 金乡县| 新乡县| 普宁市| 太康县| 象山县| 哈尔滨市| 肥西县| 盐城市| 泾源县| 白朗县| 麻阳| 鄂温| 荆州市| 静安区| 安乡县| 万州区| 衡南县| 简阳市| 靖宇县| 凤阳县| 花莲市| 临高县| 禹城市| 武山县| 蒙城县| 惠来县| 林州市| 上杭县| 新乐市| 平安县| 化德县| 同心县| 石棉县| 伊春市| 阿勒泰市| 乌拉特中旗| 巴林右旗| 高碑店市| 博白县|