Javascript @使用typescript在vue中注入不工作
我试图在vue中注入服务,如下所示 在more-information.component.ts中Javascript @使用typescript在vue中注入不工作,javascript,typescript,vue.js,dependency-injection,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 defaul
@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>) {
}
}
从“axios”导入axios;
从“vuex”导入{Store};
导出默认类MoreInformationService{
构造函数(私有$store:store){
}
}
但是我在控制台中得到了以下警告,它并没有像预期的那样工作
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>
webpack-internal:///./node_modules/vue/dist/vue.esm.js:629 [Vue warn]:未找到注入“moreInformationService”
发现于
--->位于src/main/webapp/app/MSB/products/more-information/more-information.vue
位于src/main/webapp/app/MSB/layouts/BlankLayout.vue
位于src/main/webapp/app/app.vue
我忘了使用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
});
从“@/products/more information/more information.service”导入MoreInformationService;
新Vue({
el:“#应用程序”,
组件:{App},
模板:“”,
路由器,
提供:{
moreInformationService:()=>新的moreInformationService(商店)
},
商店
});
如果有更好的选择,请回答 什么是
@Inject
?是vue property decorator提供的吗?该错误意味着未使用provide
或其他方式注册依赖项。请提供,而不仅仅是单独的代码段。对不起,这是我的错误,我忘了使用提供注册依赖项。但是注册类。它可以由这个新的Vue修复({el:'#app',组件:{app},模板:'',路由器,提供:{moreInformationService:()=>new moreInformationService()});还有其他更好的方法解决这个问题吗?这就是我希望解决的方法。考虑一下吧,欢迎回答。