uni-app中报 Uncaught TypeError: Cannot read properties of undefined (reading ‘localStorage‘)

foreword

I found a problem in the base debugging today, and record it here.
I used window.localStorage.getItem("token")a function to get the token here. There is no problem when debugging locally, but this error is reported in the base debugging.
insert image description here
The reason may be that localStorage cannot be used directly in uni-app.

solution

1. Installmp-storage

npm install mp-storage

insert image description here
2. InstalllocalStorage

npm install localStorage

insert image description here
3. Configure vue.config.js

module.exports = {
    
    
	configureWebpack:{
    
    
		plugins: [
      new webpack.ProvidePlugin({
    
    
        'localStorage': ['mp-storage','localStorage'],
        'window.localStorage': ['mp-storage', 'localStorage']
      })
		]
	}
}

Finished, check the effect, the error message disappeared.

Supongo que te gusta

Origin blog.csdn.net/weixin_44748171/article/details/130216918
Recomendado
Clasificación