我有这个简单的角度1.x模块:
'use strict'; angular.module('testModule',[]) .factory('testService',function($q,$log,$interval,$rootScope){ //Create a service object that we'll build up with methods and eventually //return. var service = {}; service.test = 'test'; return service; });
我有这个角度2模块引用角度1.x $注入器来获得角度1.x模块作为提供者:
import { NgModule } from '@angular/core'; import { browserModule } from '@angular/platform-browser'; import { UpgradeModule } from '@angular/upgrade/static'; import { AppComponent } from './app.component'; @NgModule({ imports: [ browserModule,UpgradeModule ],declarations: [ AppComponent ],bootstrap: [ AppComponent ],providers: [{ provide: 'testModule',useFactory: (i: any) => i.get('testModule'),deps: ['$injector'] }] }) export class AppModule { ngdobootstrap() {} }
我有我的角度2组件,通过依赖注入请求角度1.x模块:
import { Component,Inject } from '@angular/core'; @Component({ selector: 'my-app',template: `<h1>Hello {{name}},my name is {{myName}}</h1>` }) export class AppComponent { name = 'Angular'; constructor(@Inject('testModule') testModule: any) { this.myName = testModule.test; } }
然后我引导角度2模块和角度1.x模块
import { platformbrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformbrowserDynamic().bootstrapModule(AppModule).then(platformRef => { const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule; upgrade.bootstrap(document.body,['testModule'],{strictDi: true}); });
然后我得到这个错误TypeError:无法读取未定义的属性’get’
at useFactory是在arrow函数中发生的,该函数指定为我在angular 2模块中提供者定义的“useFactory”属性
providers: [{ provide: 'testModule',deps: ['$injector'] }]
我认为有一些我不了解如何在混合角度1 2应用中初始化角度1.x喷射器.您可以看到我在通过脚本标记中拉出角度1.x,否则我的角度1.x模块将抛出关于“角度”未定义的错误,我怀疑问题可能与此有关.如果有人能提供指导,我会很感激.
这是羽毛球:https://plnkr.co/edit/wdR7K4rDKoF8L0pfQypM?p=info
TypeError: Cannot read property ‘get’ of undefined
at useFactory (07001)
at AppModuleInjector.get (/AppModule/module.ngfactory.js:140:65)
at AppModuleInjector.getInternal (/AppModule/module.ngfactory.js:192:46)
at AppModuleInjector.NgModuleInjector.get (07002)
at CompiledTemplate.proxyViewClass.AppView.injectorGet (07003)
at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (07004)
at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/AppModule/AppComponent/host.ngfactory.js:15:63)
at CompiledTemplate.proxyViewClass.AppView.createHostView (07005)
at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (07006)
at ComponentFactory.create (07007)
at ApplicationRef_.bootstrap (07008)
at eval (07009)
at Array.forEach (native)
at PlatformRef_._moduledobootstrap (070010)
at eval (070011)
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。