gpt4 book ai didi

angularjs - 在引导混合 angular 1 + 2 应用程序时,如何初始化 angular 1 注入(inject)器?

转载 作者:行者123 更新时间:2023-12-01 04:50:38 26 4
gpt4 key购买 nike

为了重现这个问题,我 fork 了 Angular Quickstart plunker。

我有这个简单的 Angular 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;
});

我有这个 angular 2 模块,它引用了 angular 1.x $injector 来获取 angular 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() {}
}

我有我的 angular 2 组件,它通过依赖注入(inject)请求 angular 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;
}
}

然后我引导 angular 2 模块和 angular 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: Cannot read property 'get' of undefined
at useFactory
这是在我在 angular 2 模块中指定为提供程序定义的“useFactory”属性的箭头函数中发生的
providers: [{
provide: 'testModule',
useFactory: (i: any) => i.get('testModule'),
deps: ['$injector']
}]

我认为关于如何在混合 angular 1 + 2 应用程序中初始化 angular 1.x 注入(inject)器,我有一些不明白的地方。你可以看到我正在通过脚本标签拉入 angular 1.x,否则我的 angular 1.x 模块会抛出关于“angular”未定义的错误,我怀疑问题可能与此有关。如果有人可以提供指导,我将不胜感激。

这是plunker: https://plnkr.co/edit/wdR7K4rDKoF8L0pfQypM?p=info

这是调用堆栈的错误:

TypeError: Cannot read property 'get' of undefined at useFactory (https://run.plnkr.co/IgsjEBjD2d29O3YH/app/app.module.ts!transpiled:32:56) at AppModuleInjector.get (/AppModule/module.ngfactory.js:140:65) at AppModuleInjector.getInternal (/AppModule/module.ngfactory.js:192:46) at AppModuleInjector.NgModuleInjector.get (https://unpkg.com/@angular/core/bundles/core.umd.js:8918:48) at CompiledTemplate.proxyViewClass.AppView.injectorGet (https://unpkg.com/@angular/core/bundles/core.umd.js:12319:49) at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (https://unpkg.com/@angular/core/bundles/core.umd.js:12699:53) at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/AppModule/AppComponent/host.ngfactory.js:15:63) at CompiledTemplate.proxyViewClass.AppView.createHostView (https://unpkg.com/@angular/core/bundles/core.umd.js:12275:25) at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (https://unpkg.com/@angular/core/bundles/core.umd.js:12683:56) at ComponentFactory.create (https://unpkg.com/@angular/core/bundles/core.umd.js:7710:29) at ApplicationRef_.bootstrap (https://unpkg.com/@angular/core/bundles/core.umd.js:8677:61) at eval (https://unpkg.com/@angular/core/bundles/core.umd.js:8506:93) at Array.forEach (native) at PlatformRef_._moduleDoBootstrap (https://unpkg.com/@angular/core/bundles/core.umd.js:8506:46) at eval (https://unpkg.com/@angular/core/bundles/core.umd.js:8458:31)

最佳答案

我遇到了同样的问题,并在 github 上找到了我的案例的解决方案.
看看更新的 plunker .请注意,您的代码中还有其他不相关的问题。已评论 <script src="test-module.js"></script> testService 的隐式注入(inject)参数.
希望它会帮助你。

关于angularjs - 在引导混合 angular 1 + 2 应用程序时,如何初始化 angular 1 注入(inject)器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41412462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com