【问题标题】:TypeScript compiler can't find injected property in Vue Object ComponentTypeScript 编译器在 Vue 对象组件中找不到注入的属性
【发布时间】:2018-09-30 13:21:00
【问题描述】:

我正在编写几个工作示例,其中一个让我束手无策的是注入在 Vue 引导期间提供的服务。

这个“有效”(我可以访问它,它可以编译和运行),我的 JavaScript 版本和 Class-Component TypeScript 版本都没有问题或抱怨,但编译器抱怨 this.sampleService 没有将 Vue 对象 API 与 TypeScript 一起使用时存在于我的组件中。

我错过了什么吗?

<template>
  <div class="app">
    {{message}} <fake-button :text="buttonText" @some-click-event="handleClickEvent"></fake-button>
  </div>
</template>

<style lang="scss">
  .app {
    $background-color: #9f9;
    $foreground-color: #000;
    background: $background-color;
    color: $foreground-color;
  }
</style>

<script lang="ts">
  import Vue from 'vue'

  const App = Vue.extend({
    components: {
      FakeButton: () => import('@client/components/fake-button/fake-button-object-typescript.vue')
    },
    data: function () {
      return {
        message: 'Hello World - App Object TypeScript',
        buttonText: 'Click Me'
      }
    },
    inject: {
      sampleService: 'sampleService'
    },
    methods: {
      handleClickEvent(someVal?: string) {
        console.log('App', 'handleClickEvent', someVal);
      }
    },
    beforeCreate() {
      console.log('App', 'beforeCreate');
    },
    created() {
      console.log('App', 'created');
    },
    mounted() {
      console.log('App', 'mounted');
      // TODO: While this compiles, TypeScript complains that it doesn't exist
      console.log('this.sampleService.getDate()', this.sampleService.getDate());
    }
  });

  export default App;
</script>

ERROR in vue-test/src/client/containers/app/app-object-typescript.vue.ts
    [tsl] ERROR in vue-test/src/client/containers/app/app-object-typescript.vue.ts(35,56)
          TS2339: Property 'sampleService' does not exist on type 'CombinedVueInstance<Vue, { message: string; buttonText: string; }, { handleClickEvent(someVal?: s...'.

【问题讨论】:

    标签: api typescript object vue.js inject


    【解决方案1】:

    尝试添加provide。检查下面的示例。我建议使用vue-property-decorator,因为您使用的是typescript

    inject: {
          sampleService: 'sampleService'
        },
    provide () {
        return {
          sampleService: this.sampleService,
        }
      }
    

    【讨论】:

    • 你使用的是打字稿还是javascript?
    • 这个问题是针对 TypeScript 的。
    【解决方案2】:

    我对这个问题的解决方案是为注入创建一个接口。在您的示例中,这将类似于以下内容:

    <script lang="ts">
    import { SampleServiceInjection } from '...';
    
    const App = ( Vue as VueConstructor<Vue & SampleServiceInjection> ).extend({
      inject: {
        sampleService: 'sampleService'
      } as Record<keyof SampleServiceInjection, string>,
      // etc.
    });
    </script>
    

    您也可以在提供服务的组件中使用该接口:

    export interface SampleServiceInjection {
      sampleService: SampleService; // or whatever type your service has
    }
    
    export default Vue.extend({
      provide(): SampleServiceInjection {
        return {
          sampleService: new SampleService(),
        };
      },
      // etc.
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 1970-01-01
      • 2014-03-25
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 2018-04-23
      相关资源
      最近更新 更多