react全局变量的设置
新建一个 util文件夾 ---> tool.jsxwindow._= {/*** 存储localStorage*/setStore:(name, content) =>{if (!name) return;if (typeof content !== 'string') {content = JSON.stringify(c...
·
新建一个 util文件夾 ---> tool.jsx
window._= {
/**
* 存储localStorage
*/
setStore:(name, content) =>{
if (!name) return;
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
window.localStorage.setItem(name, content);
},
/**
* 获取localStorage
*/
getStore:(name) => {
if (!name) return;
return window.localStorage.getItem(name);
},
/**
* 清除localStorage
*/
clearStore:() => {
window.localStorage.clear();
},
getQueryStringByName: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
var context = "";
if (r != null)
context = r[2];
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "" : context;
}
}
在入口文件app.jsx里面引入
import "util/tool.jsx";
然后在其余的组件里面就可以访问到这个变量对象:_
更多推荐
所有评论(0)