-
在models文件夹下的模型文件中使用localStorage,启用ssr打包,报错localStoragei not defined采用了官网该的类似的解决方法:类似解决方法,但是会报错FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory,没有解决该问题代码如下
import {IModelTheme, TModelThemeState} from "@/lib/model_theme";
// import {isBrowser} from "@umijs/utils";
const Theme:IModelTheme = {
state: "light",
reducers: {
setState(state, action){
return action.payload
}
},
effects:{
* asyncSetState(action, {put, select}){
// 更改主题
const {payload} = action;
yield put({
type: "setState",
payload
})
const {theme} = yield select()
// if(!isBrowser()){
//
// }
localStorage.setItem("theme", theme);
const html = document.querySelector("html");
if(html){
html.className = theme
}
}
},
subscriptions:{
init(e){
// if(!isBrowser()){
//
// }
// 初始化主题
let theme = localStorage.getItem("theme");
let html = document.querySelector("html");
if(theme){
e.dispatch({
type: "setState",
payload:theme as TModelThemeState
})
if(html){
html.className = theme
}
}else {
localStorage.setItem("theme", "light");
}
}
}
}
export default Theme 以上代码放到isBrower函数进行判断之后,启用ssr打包,会报另外一个错误:FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 请教一下哪位仁兄碰到过这种情况,或者有没有解决的方法,谢谢 |
Beta Was this translation helpful? Give feedback.
Answered by
sorrycc
Jan 17, 2021
Replies: 1 comment 1 reply
-
参考 https://stackoverflow.com/questions/52474208/react-localstorage-is-not-defined-error-showing |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
sorrycc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考 https://stackoverflow.com/questions/52474208/react-localstorage-is-not-defined-error-showing