@Inject在打字稿中无法正常工作

时间:2020-04-05 09:45:52

标签: javascript typescript vue.js dependency-injection

我正在尝试按以下方式在vue中注入服务。

more-information.component.ts

@Inject('moreInformationService') private moreInformationService: () => MoreInformationService;

more-information.service.ts

import axios from 'axios';
import { Store } from 'vuex';
export default class MoreInformationService {
  constructor(private $store: Store<any>) {
  }    
}

但是在控制台中出现了以下Waring,并且无法按预期运行

webpack-internal:///./node_modules/vue/dist/vue.esm.js:629 [Vue warn]: Injection "moreInformationService" not found

found in

---> <MoreInformation> at src/main/webapp/app/MSB/products/more-information/more-information.vue
       <BlankLayout> at src/main/webapp/app/MSB/layouts/BlankLayout.vue
         <App> at src/main/webapp/app/app.vue
           <Root>

1 个答案:

答案 0 :(得分:1)

我忘记使用provide

注册服务

以下更改解决了我的问题,

import MoreInformationService from '@/products/more-information/more-information.service';

new Vue({
  el: '#app',
  components: { App },
  template: '<App/>',
  router,
  provide: {
    moreInformationService: () => new MoreInformationService(store)
  },
  store
});

如果有更好的选择,请回答。

相关问题