加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:08:51 所属栏目:安全 来源:网络整理
导读:为了重新创建这个问题,我分叉了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
为了重新创建这个问题,我分叉了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',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的隐式注入参数. 希望它会对你有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读