angularjs – 在启动混合角度1 2应用程序时,如何初始化角度1喷射器?

为了重新创建这个问题,我分叉了Angular Quickstart plunker.

我有这个简单的角度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',
    useFactory: (i: any) => i.get('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)

最佳答案 我遇到了同样的问题,并在
github上为我的案例找到了解决方案.

请查看更新的
plunker.请注意,您的代码中还有其他不相关的问题.评论< script src =“test-module.js”>< / script>和testService的隐式注入参数.

希望它会对你有所帮助.

点赞