vue computed动态监听 router变化 取值错误

75 阅读1分钟

问题描述:计算监听router变化,在onBeforeUnmount 中去这个值得时候,值为undefind

const saveStorageKey = reactive({
    SKIN_TYPE:computed(()=> get(router.currentRoute.value.params, 'id') +'_skin-type')
})

onBeforeUnmount(() => {
    const key = get(router.currentRoute.value.params, 'id') +'_skin-type';
    appSessionStore(key, trans.skin_type)
    或者
    appSessionStore(saveStorageKey.SKIN_TYPE, trans.skin_type)
    
   这里 get(router.currentRoute.value.params, 'id') 或 saveStorageKey.SKIN_TYPE 获取的值为
   undefind,
})

错误理解:大概是router push到新界面是,路由改变,且路由里面不在有id参数,所以SKIN_TYPE已经获取不到值,导致获取值失败