1. window.location.href = 'url' ; 改变地址进行跳转
2. window.location.replace('url') ; 将地址替换成新的url, 该方法通过制定的url替换,当前缓存在历史里(客户端)的项目,因此当使用了replace方法之后,你不能通过 前进 和 后退 , 来访问已经被替换的url, 这个特点对一些过渡页面非常有用!
例:
点击登录按钮,会触发跳转的方法,这里就会进入缓存的业务页面的url地址
// 登录页 ==> 触发登录方法
if (!Utils.launchRedirect.existLaunchPageByUrlPath()){
uni.redirectTo({
url: Config.indexRoute
})
}
else{
Utils.launchRedirect.redirectlaunchPage()
}
// launchRedirect.js
//重定向页面
function redirectlaunchPage() {
if (existLaunchPageByUrlPath()){
// window.location.replace ==> 将目前浏览器的地址替换掉 uni.getStorageSync('launchPage') ==> 获取存储中对应的key
// 使用 window.location.replace 方法之后,不能前进、后退来访问已被替换的url
window.location.replace(uni.getStorageSync('launchPage'))
// 从本地缓存中同步移除指定 key
uni.removeStorageSync("launchPage")
}
}
//通过是否缓存相应的入口业务页面
function existLaunchPageByUrlPath() {
try {
const value = uni.getStorageSync('launchPage');
if (value && value!="") {
return true
}
return false
} catch (e) {
return false
}
}
3. window.location.reload() ; 强制刷新页面