类型上不存在属性 - 带有打字稿的 vue [英] property not exist on type - vue with typescript

查看:39
本文介绍了类型上不存在属性 - 带有打字稿的 vue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 js 代码转换为 ts.我的尝试如下.

I am trying to convert the js code to ts. My attempt is as below.

<script lang="ts">
 export default {
  data() {
    return {
      isShow: false as boolean
    }
  },
  methods: {
    openCalendar() : void {
      this.isShow = true;
    },
  }
}
</script>

但是当我运行应用程序时,它在控制台中抛出一个错误,说 {open(): void;} 类型上不存在属性 isShow.我哪里出错了,我该如何解决?

But when I run the app it's throws an error in the console saying Property isShow does not exist on type {open(): void;}. Where I was got wrong and how can I fix it?

推荐答案

要获得类型推断,请尝试使用 Vue.extend({}) 包装您的选项:

To get types inference try to wrap your options with Vue.extend({}) :

<script lang="ts">
import Vue from 'vue'  
export default Vue.extend({
data() {
    return {
      isShow: false as boolean
    }
  }
  methods: {
    openCalendar() : void {
      this.isShow = true;
    },
 },
})
</script>

这篇关于类型上不存在属性 - 带有打字稿的 vue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆