我的项目是父项目和子项目分开时,然后当子项目需要用到父项目登录的信息时,共享参数的几个方法
方法一:
sessionstorage信息共享,父项目中在登录时query里传递相关信息,如userName,token等信息,然后在子项目的html中定义函数去获取然后存到sessionstorage
<script>
// 解析父项目中query里的参数
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
var token = getQueryVariable('t')
var userName = getQueryVariable('userName'); // 获取登录人userName
if(userName){
sessionStorage.setItem('userName',userName)
}
if(token){
sessionStorage.setItem('token',token)
}
</script>
方法二:
从url里携带过来然后进行解析,然后存在vuex或者本地存储里
<script>
function getUrlKey(name) {
return (
decodeURIComponent(
(new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
location.href
) || [, ""])[1].replace(/\+/g, "%20")
) || null
);
}
const orgId=getUrlKey("organizationId")?Base64.decode(getUrlKey("organizationId")):(getStore({name:'orgId'})?getStore({name:'orgId'}):'')
const userId=getUrlKey("userId")?Base64.decode(getUrlKey("userId")):(getStore({name:'userId'})?getStore({name:'userId'}):'')
const mobile=getUrlKey("mobile")?getUrlKey("mobile"):(getStore({name:'mobile'})?getStore({name:'mobile'}):'')
</script>