vue2升级vue3:composition api中监听路由参数改变
vue2 的watch回顾
我们先回顾一下vue2中watch
《watch性能优化:vue watch对象键值说明-immediate属性详解》
《vue中methods/watch/computed对比分析,watch及computed原理挖掘》
watch和computed很相似,watch用于观察和监听页面上的vue实例,当然在大部分情况下我们都会使用computed,但如果要在数据变化的同时进行异步操作或者是比较大的开销,那么watch为最佳选择。watch为一个对象,键是需要观察的表达式,值是对应回调函数。值也可以是方法名,或者包含选项的对象。
如果在data中没有相应的属性的话,是不能watch的,这点和computed不一样。
适用场景:
watch实现原理
this.$watch()手动注入
比如我们可以手动注入监听器,只有一定条件时,才需要监听,这个也可以大大的提升性能
1234567891011121314151617created() {
this
.debounceEvent = debounce(
this
.demoEvent,
this
.curretnDelay,
);
// 只有图表编辑页面,才需要监听
if
(
this
.$route.name ===
'chartDialog'
) {
this
.$watch(
'dataOptions'
, {
handler() {
// 只有图表编辑页面,才需要监听
this
.renderData(
true
);
console.log(
'watch dataOptions'
);
},
deep:
true
,
});
}
}
vue2 的watch不再赘述
vue3 composition api 监听路由变化
组件内路由导航守卫
使用组件内路由导航守卫 onBeforeRouteUpdate
1 2 3 4 5 | setup(){ onBeforeRouteUpdate( to =>{ // console.log(to.params, to.query) }) } |
推荐使用这个方法
导航守卫-全局后置钩子
路由守卫中监听路由参数,再使用计算属性导出,可全局使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import { RouteParams, LocationQueryRaw } from 'vue-router' ; import { computed, reactive } from 'vue' ; import router from '@/router/index' ; const routeData = reactive<{params: RouteParams, query: LocationQueryRaw}>({ params: {}, query: {} }); router.afterEach((route) => { routeData.params = route.params; routeData.query = route.query; }); export function useRouteParam() { return computed(() => routeData.params); } export function useRouteQuery() { return computed(() => routeData.query); } |
在页面内使用
1 2 3 4 5 6 | export default defineComponent({ setup() { const params = useRouteParam(); const spaceId = params.value.space_uid; } } |
z'合并时不推荐的。没有必要全局
将参数与路由解耦,注入到组件的props中去进行监听
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // router/index.js const router = new VueRouter({ routes: [{ path: 'article/:articleId' component: Article, //props: (route) => {articleId: route.params.articleId} //原文返回对象没加小括号包裹,具有二义性 props: (route) => ({articleId: route.params.articleId}) }] }) // your component setup(props) { const article = reactive({...}); function fetchArticle(id) { //assign article.. } watch(() => props.articleId, fetchArticle) return { article }; } |
需要立即执行回调函数,可以引入watchEffect
需要立即执行回调函数,可以引入watchEffect,不需要传参数直接把回调扔进去,代码简介明了(回调中自动收集依赖,不要要手动指定,且第一次回调立即触发)
1 2 3 4 5 6 7 8 | import { watchEffect } from "vue" // ··· setup(props){ function initData(){ // 使用了props } watchEffect(initData) //initData立即执行,且当props中依赖的数据改变时,会自动执行 } |
在组件内watch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | setup() { const article = reactive({...}); function fetchArticle(id) { //assign article.. } const route = useRoute(); watch( () => route.params, () => { const { articleId } = route.params; if (articleId) { fetchArticle(articleId); } }, { immediate: true , deep: true } ); } |
官方文档给的案例也是这个:https://router.vuejs.org/guide/advanced/composition-api.html#accessing-the-router-and-current-route-inside-setup
页面跳转传了 params 但是无效
-
传 params 的时候要传 name
-
加入 { immediate: true, deep: true } 就可以了
参考文章:
Vue3 监听路由变化 https://trycoding.fun/JavaScript/vue3-watch-route/
Vue3.0 中监听路由参数的改变方法大全 https://blog.csdn.net/qq_41777791/article/details/113100730
https://medium.com/js-dojo/watch-vue-route-params-query-with-composition-api-97b3c8c402e
转载本站文章《vue2升级vue3:composition api中监听路由参数改变》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/vue3/8860.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-07-25 web自动化测试(2):选择selenium优势?与PhantomJS/QTP/Monkey对比
2021-07-25 web自动化测试(1):再谈UI发展史与UI、功能自动化测试