欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > this.$route.path 和 location.href获取的值不一样

this.$route.path 和 location.href获取的值不一样

2024/10/25 4:16:12 来源:https://blog.csdn.net/u013994400/article/details/141924224  浏览:    关键词:this.$route.path 和 location.href获取的值不一样

引言:

vue项目中动态加载路由组件this. r o u t e . p a t h 获取不正确(获取是‘ / ‘而不是‘ / f o r g e t P w d ’)浏览器中的路由是 / f o r g e t P w d ,路由全局钩子 r o u t e r . b e f o r e E a c h 得到的 p a t h 也是 / f o r g e t P w d , 但是在页面 i n d e x . v u e 中的生命周期 c r e a t e d 获取到的 p a t h 是 / ,这个时候想到 t h i s . route.path获取不正确(获取是‘/‘而不是‘/forgetPwd’) 浏览器中的路由是/forgetPwd,路由全局钩子router.beforeEach得到的path也是/forgetPwd,但是在页面index.vue中的生命周期created获取到的path是/,这个时候想到 this. route.path获取不正确(获取是‘/‘而不是‘/forgetPwd)浏览器中的路由是/forgetPwd,路由全局钩子router.beforeEach得到的path也是/forgetPwd,但是在页面index.vue中的生命周期created获取到的path/,这个时候想到this.route.path 的底层获取逻辑是location.href,那我同时去打印location.href,得到的却和router.beforeEach里面得到的一样,也就是说 this.$route.path 和 location.href 得到的值不一样

原因:

问题出在当项目启动时,懒加载方式,路由加载注册的时候没有加载path:‘/forgetPwd’下的$route
查看路由配置 ,懒加载方式
const forgetPwd= r => require.ensure([], () => r(require(’@/components/forgetPwd.vue’)), ‘forgetPwd’)
改成
import forgetPwd from ‘@/components/login/forgetpwd.vue’
这时候在查看路由,发现完美解决

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com