【问题标题】:VueJS 2.5 and TypeScript enhancementsVueJS 2.5 和 TypeScript 增强功能
【发布时间】:2018-03-26 17:45:33
【问题描述】:

VueJS 2.5 出来了,一直在等TypeScript enhancements,但是升级了需要的依赖后,真的看不到任何增强。

根据我的阅读,这应该会显示类型错误,因为 msg 是一个数字

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
  export default {
    data: function () {
      return {
        msg: 1
      }
    },
    methods: {
      test: function (): number {
        return this.msg.indexOf('App')
      }
    }
  }
</script>

但它实际上没有错误,就像以前的版本一样。有什么线索吗? TypeScript 的新功能在哪里?

【问题讨论】:

    标签: typescript vue.js


    【解决方案1】:

    拥有一个 tsconfig.json 并将“strict”设置为 true 很重要。见https://vuejs.org/v2/guide/typescript.html#Recommended-Configuration

    【讨论】:

    • "strict": true 完成这项工作,但它也会检查我不想检查的其他可能的错误。我会坚持“noImplicitThis”:真的。
    【解决方案2】:
    export default Vue.extend({
        // data
        // methods
    })
    

    Vue 组件中this 的类型推断只能在使用时发生 规范的 Vue API。

    【讨论】:

    • 抱歉,这个通知效果不好。
    【解决方案3】:

    noImplicitThis 必须在tsconfig.json 中设置为trueallowSyntheticDefaultImports 选项也可以去掉。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多