Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H5版路由守卫未能和window.location同步 #52

Closed
minkuan opened this issue Dec 11, 2019 · 12 comments
Closed

H5版路由守卫未能和window.location同步 #52

minkuan opened this issue Dec 11, 2019 · 12 comments

Comments

@minkuan
Copy link

minkuan commented Dec 11, 2019

Describe the bug
由于微信的BUG,SPA/HISTOR/H5在微信浏览器“复制链接”/“在浏览器中打开“,链接错误。解决办法:https://www.cnblogs.com/wisewrong/p/9689897.html
但uni-simple-router的路由守卫beforeEach/afterEach在生命周期中的时机似不正确,window.location.replace()或window.location.href=xxx所作的URL变更,未能同步到uni-simple-router组件的Router/Route中,导致该解决办法陷入无限刷新;因而uni-simple-router组件无法绕过微信浏览器BUG,不能在微信浏览器中使用。

@SilurianYang
Copy link
Owner

这个问题 其实可以这样解决 在H5上 使用通配符 * 然后获取到 to 对象 接着你判断下当前是否在微信浏览器打开 如果是 你可以把 to 对象的 name 和query 组装成一个对象 最好转成字符串传递 作为到此个页面的参数 然后 重新 return 出去。 这样 在这个页面后面就保留了相关信息。 完事之后在beforeEach 中判断当前 to是否带了参数 如果有预设的微信浏览器的标识 那么就拿到 query后面的参数重新 next() 一下 这样就能完成了

@minkuan
Copy link
Author

minkuan commented Dec 12, 2019

@SilurianYang 文字描述清楚达义太难了:-) 然额通配符*分支,在其他页面都精确配置路由的情况下,由于不占优先级,*分支走不到吧?求出手更准确的代码:-)

@SilurianYang
Copy link
Owner

@minkuan 那你可以把通配符的代码移植到 beforeEach 中一样可以使用 反应就是想办法 在路径过来拼接上当前页面的所有信息

@SilurianYang
Copy link
Owner

@minkuan 你可以加群讨论或者完善 uni-simple-router 群号:769241495

@minkuan
Copy link
Author

minkuan commented Dec 12, 2019

router.beforeEach((to, from, next) => {
// #ifdef H5
console.log("beforeEach..to=", to, "..from=", from, "..to.query.wxRefreshed=", to.query.wxRefreshed, "..isWx=", Consts.isWeixin());
// #endif

// #ifdef H5
uni.getSystemInfo({
success: function(res) {
console.log("successFunc..res=", res);
if (res.platform === "ios") {
console.log('ios====platform======')
if (to.name !== "home" && Consts.isWeixin()) {
// 是否有微信标识url参数?
if (!Consts.intOrStringEqual(to.query.wxRefreshed, "2")) { // 没有微信url标识
let newQuery;
if (to.query.wxRefreshed===undefined) newQuery = Object.assign(to.query, {wxRefreshed: 1});
else {
newQuery = Object.assign(to.query, {wxRefreshed: parseInt(to.query.wxRefreshed)+1})
}
next({
path: to.path,
query: newQuery
});
} else {
console.log('wxRefreshed=2')
next();
}
} else next()
}
},
complete: function() {
console.log("getSystemInfo..complete");
}
});
// #endif

// #ifndef H5
next();
// #endif
});

@SilurianYang 因H5在微信中手工刷新2次后即可复制链接/在浏览器中打开成功,以上代码尝试做2次路由。以上处理为嘛无效呢?此前在目标页mounted中尝试window.location.replace(xxx)则uni-simple-router将陷入无限刷新

@minkuan
Copy link
Author

minkuan commented Dec 12, 2019

卧槽,搞定~

@minkuan minkuan closed this as completed Dec 12, 2019
@SilurianYang
Copy link
Owner

卧槽,搞定~

哦!伙计 看起来你成功啦! 恭喜你 ! 我很希望你能贡献你的源码 放置文档 相关教程 下帮助更多人

@minkuan
Copy link
Author

minkuan commented Dec 14, 2019

卧槽,搞定~

哦!伙计 看起来你成功啦! 恭喜你 ! 我很希望你能贡献你的源码 放置文档 相关教程 下帮助更多人

没问题,你这项目及文档做的还不错。你说下格式要求,分享互助更多人

@SilurianYang
Copy link
Owner

以.md结尾的文件即可,包括标题及正文。请发布到我的邮箱📬 我会在文档首页上说明出处。感谢你的支持!

@minkuan
Copy link
Author

minkuan commented Dec 17, 2019

以.md结尾的文件即可,包括标题及正文。请发布到我的邮箱 我会在文档首页上说明出处。感谢你的支持!

简单记个文档,没看到你邮箱。。btw 留下你邮箱地址吧,我发你邮箱

@jpgaus
Copy link

jpgaus commented Feb 24, 2020

以.md结尾的文件即可,包括标题及正文。请发布到我的邮箱 我会在文档首页上说明出处。感谢你的支持!

简单记个文档,没看到你邮箱。。btw 留下你邮箱地址吧,我发你邮箱

遇到同样的问题,微信刷新死循环。您能发一份完整的文档吗? 我在官方文档教程没找到,非常感谢!

@SilurianYang
Copy link
Owner

@jpgaus 这是楼主提供的解决方案 我上传上来 希望帮助更多需要的人
uni-simple-router解决IOS微信浏览器URL-BUG.docx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants