vue路由監(jiān)聽的一些常用方式
場景:
我們在做項目時,會遇到部分需求是根絕路由的切換來判斷某些值,下面介紹部分常用的監(jiān)聽方式
一、 通過 watch 進(jìn)行監(jiān)聽
// 方式1、監(jiān)聽路由 $route 變化 一般監(jiān)聽
export default{
watch: {
$route(to, from){
console.log('路由變化了')
console.log('當(dāng)前頁面路由:' + to.path);
console.log('上一個路由:' + from);
},
}
}
// 方式2、監(jiān)聽路由 $route 變化, 使用handler函數(shù) 深度監(jiān)聽路由
export default{
watch: {
'$route': { // $route可以用引號,也可以不用引號 監(jiān)聽的對象
handler(to, from){
console.log('路由變化了')
console.log('當(dāng)前頁面路由:' + to);
console.log('上一個路由:' + from);
},
deep: true, // 深度觀察監(jiān)聽 設(shè)置為 true
immediate: true, // 第一次初始化渲染就可以監(jiān)聽到
}
}
}
// 方式3、監(jiān)聽路由 $route 變化,觸發(fā)methods里的方法
export default{
watch: {
'$route': 'initData'
},
methods: {
initData(){
console.log('路由變化了')
}
}
}
// 方式4、監(jiān)聽路由的 path 變化
export default{
watch: {
'$route.path'(toPath, fromPath){
console.log('路由變化了')
console.log('當(dāng)前頁面路由地址:' + toPath)
console.log('上一個路由地址:' + fromPath)
},
}
}
// 方式5、監(jiān)聽路由的 path 變化, 使用handler函數(shù)
export default{
watch: {
'$route.path': {
handler(toPath, fromPath){
console.log('路由變化了')
console.log('當(dāng)前頁面路由地址:' + toPath)
console.log('上一個路由地址:' + fromPath)
},
deep: true, // 深度監(jiān)聽
immediate: true, // 第一次初始化渲染就可以監(jiān)聽到
}
}
}
// 方式6、監(jiān)聽路由的 path 變化,觸發(fā)methods里的方法
export default{
watch: {
'$route.path': 'initData'
},
methods: {
initData(){
console.log('路由變化了')
}
}
}二、通過鉤子函數(shù)beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave進(jìn)行監(jiān)聽
export default{
beforeRouteEnter(to, from, next){
// 渲染該組件前調(diào)用這個鉤子,因此組件還未被創(chuàng)建,不能獲取this
console.log(this) // 結(jié)果為:undefined
console.log('beforeRouteEnter')
next()
},
beforeRouteUpdate(to, from, next){
//這個組件是被復(fù)用的時候調(diào)用,比如帶有動態(tài)參數(shù)的路由跳轉(zhuǎn):/add/11 跳轉(zhuǎn)到 /detail/12
console.log(this) // 可以訪問this
console.log('beforeRouteUpdate')
next()
},
beforeRouteLeave(to, from, next){
// 導(dǎo)航離開當(dāng)前路由的時候被調(diào)用,this可以被訪問到
console.log(this) // 可以訪問this
console.log('beforeRouteLeave')
next()
},
}三、全局路由監(jiān)聽 this.$router.beforeEach
// 方式1、在App.vue的create中進(jìn)行全局路由監(jiān)聽
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)聽
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)聽的一些常用方式的文章就介紹到這了,更多相關(guān)vue路由監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vuecli3打包后出現(xiàn)跨域問題,前端配置攔截器無效的解決
這篇文章主要介紹了vuecli3打包后出現(xiàn)跨域問題,前端配置攔截器無效的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Vue使用openlayers實現(xiàn)繪制圓形和多邊形
這篇文章主要為大家詳細(xì)介紹了Vue如何使用openlayers實現(xiàn)繪制圓形和多邊形,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起動手嘗試一下2022-06-06
elementui之el-table-column日期格式顯示方式
文章介紹了如何使用formatter屬性對表格某一列的內(nèi)容進(jìn)行日期格式化,通過綁定日期格式化的方法實現(xiàn),展示了前端代碼的模板和方法,并給出了前端效果的展示2024-12-12
import.meta.glob() 如何導(dǎo)入多個目錄下的資源(最新推薦)
import.meta.glob() 其實不僅能接收一個字符串,還可以接收一個字符串?dāng)?shù)組,就是匹配多個位置,本文給大家介紹import.meta.glob() 如何導(dǎo)入多個目錄下的資源,感興趣的朋友一起看看吧2023-11-11
IOS上微信小程序密碼框光標(biāo)離開提示存儲密碼的完美解決方案
ios密碼框輸入密碼光標(biāo)離開之后會提示存儲密碼的彈窗,關(guān)于這樣的問題怎么解決呢,下面給大家分享IOS上微信小程序密碼框光標(biāo)離開提示存儲密碼的完美解決方案,感興趣的朋友一起看看吧2024-07-07
Vue的watch和computed方法的使用及區(qū)別介紹
Vue的watch屬性可以用來監(jiān)聽data屬性中數(shù)據(jù)的變化。這篇文章主要介紹了Vue的watch和computed方法的使用及區(qū)別,需要的朋友可以參考下2018-09-09

