从1.6.6升级到6时$ Injector错误

时间:2018-08-31 15:23:58

标签: angularjs angular ng-upgrade angular-upgrade angular-hybrid

我使用Webpack 4从Angular.Js 1.6.6升级到Angular 6:


    import 'core-js/es7/reflect';
    import 'zone.js';
    import 'reflect-metadata';
    import 'rxjs';
    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { Routes, RouterModule } from '@angular/router';
    import { BrowserModule } from '@angular/platform-browser';
    import { UpgradeModule, downgradeComponent, downgradeInjectable, setAngularJSGlobal } from '@angular/upgrade/static';
    import { module } from './app.module.ajs';
    import './config/routes';
    import AppComponent from './components/app/app';
    import Application from './directives/application/application';
    import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
    console.log('NgModule');
    const appRoutes: Routes = [
    ];
    @NgModule({
      declarations: [AppComponent, Application],
      entryComponents: [AppComponent],
      imports: [
        BrowserModule,
        FormsModule,
        RouterModule.forRoot(appRoutes),
        UpgradeModule
      ]
    })
    class AppModule {
      constructor(public upgrade: UpgradeModule) {
        console.log('AppModule constructor');
      }
      ngDoBootstrap() {
        console.log('AppModule ngDoBootstrap');
      }
    }
    console.log('setAngularJSGlobal ...');
    setAngularJSGlobal(window['angular']);
    console.log('setAngularJSGlobal!!!');
    import main = require('./main');
    console.log('Before bootstrapModule module.name: ', module.name);
    platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
      console.log('Hybrid mode: Angular + Angular.Js');
      module.directive('appRoot', downgradeComponent({ component: AppComponent }));
      const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
      console.log('bootstrapModule: ', module.name);
      main['launchAngular']();
      upgrade.bootstrap(document.body, [module.name], { strictDi: true });
    });

创建了自定义别名,为tsconfig添加了正确的路径,Webpack构建通过并产生了输出,但是当应用程序在浏览器中启动时,控制台中的错误弹出窗口:ngRoute is missing or incorrect ng module components 没有正确完成什么以及如何解决此错误?

1 个答案:

答案 0 :(得分:2)

修复了一些Webpack别名。替换为:使用Axios的Angular-Resource,使用UI-Router / Angular-Hybrid的Angular-Route,将重构的控制器重构为组件/指令和服务,摆脱了$ rootScope和Magic的事情:升级开始起作用。