Vue 3 打字稿构建 'this is undefined'

Vue 3 Typescript Build 'this is undefined'

我是 Vue 的新手,正在使用 Vite 和 TypeScript 来制作我的项目。每当我去构建时,我都会收到一页错误,其中大部分由我的模板部分中的 Object is possibly 'undefined' 组成,例如:

<input :value="this.$store.state.shareUrl"/>

忽略类型检查并查看构建的页面会生成一个包含错误 TypeError: Cannot read properties of undefined (reading '$store') 的空白页面,但是该页面在开发版本中完全可以正常工作。关于如何解决的任何想法?

在 Vue 模板中,this 是隐式的(这就是为什么 console.log 在模板中不起作用),因此您可以跳过

<input :value="$store.state.shareUrl"/>