getCurrentInstance与ts结合使用 vue3项目中,如果不用ts这样使用是没问题的 1 const { proxy } = getCurrentInstance() 在ts中使用会报错:报错:...类型ComponentInternalInstance | null 我们在项目中一般会用
getCurrentInstance与ts结合使用vue3项目中,如果不用ts这样使用是没问题的
在ts中使用会报错:报错:...类型“ComponentInternalInstance | null” 我们在项目中一般会用到很多getCurrentInstance()方法,直接封装一下 创建useCurrentInstance.ts文件:
组件内使用:
vue3+ts使用getCurrentInstance报错vue3中没有this + 各种api的方法 vue3提供的方法,创建类似于this的实例。
这种只适合本地调试,运行到线上就会报错,报错详情为: 类型“ComponentInternalInstance | null”上不存在属性“proxy”。ts(2339) 然后下面会报这个错误
原因: getCurrentInstance()的返回类型存在null所以在此处添加断言即可。 在proxy后面添加?来过滤null的结果,即:
|
2021-06-04
2019-01-10
2019-02-17
2021-09-12
2021-09-30