1. 错误代码
参考:https://www.mmbyte.com/article/92849.html
1 state.localuserInfo = JSON.parse(localStorage.getItem('userInfo')) //Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'
'string | 类型的参数 null' 不能分配给“字符串”类型的参数。类型 'null' 不能分配给类型 'string'
修复
1 state.localuserInfo = JSON.parse(localStorage.getItem('userInfo') || '')
2.错误提示
参考https://blog.csdn.net/weixin_39651562/article/details/112597090
import Cookies from 'js-cookie' //Could not find a declaration file for module 'js-cookie'."/Users/fanshun/Desktop/project/vue3/vue3_elementp lus /node_modules/js-cookie/ index. js'implicitly has an "any' type.Try npm i --save-dev @types/js-cookie* if it exists or add a new declaration (.d. ts) filecontaining * declare module 'js-cookie'; Vetur(7016)
有的npm包使用原生js没问题,换ts后某些包会报“Could not find a declaration file for module”的错误。
有以下两种方式解决
1、下载 @type/报错包(部分包开发者可能没有上传自己的.d.ts代码到npm分支,这时会报错说找不到这个包,别急看下一步)
2、最直接简单有效的解决方法:项目根目录下shims-vue.d.ts文件
1 declare module '*.vue' {
2 import type { DefineComponent } from 'vue'
3 const component: DefineComponent<{}, {}, any>
4 export default component
5 }
6
7 declare module 'js-cookie' //加上这一句