简体   繁体   English

AngularJS到Angular - NgUpgrade

[英]AngularJS to Angular - NgUpgrade

I have a app running AngularJS 1.5 and I'm trying to use NgUpgrade to start migrating old components to Angular. 我有一个运行AngularJS 1.5的应用程序,我正在尝试使用NgUpgrade开始将旧组件迁移到Angular。

My first step it's to have both frameworks running side by side. 我的第一步是让两个框架并排运行。 But when using NgUpgrade, I'm getting the following error: 但是当使用NgUpgrade时,我收到以下错误:

Unhandled Promise rejection: [$injector:modulerr] Failed to instantiate module $$UpgradeModule due to:
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module undefined due to:
Error: [ng:areq] Argument 'module' is not a function, got undefined

Basically I have an app.module.ts for Angular and app.js for AngularJS. 基本上我有Angular的app.module.ts和AngularJS的app.js。

Following angular documentation, I created a main.ts to bootstrap both frameworks. 在角度文档之后,我创建了一个main.ts来引导两个框架。

import { AppModule } from './app.module';

import { UpgradeModule } from '@angular/upgrade/static';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
    console.log('Bootstrap both Angular and AngularJS ');
    const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
    upgrade.bootstrap(document.body, ['myApp'], {strictDi: true});
});



For creating my bundle, I'm using webpack.

This is the current state of our module. 这是我们模块的当前状态。 Most of the code is irrelevant to bootstrap both framework, since it's related with Redux. 大多数代码都与引导两个框架无关,因为它与Redux有关。 First, you need to remove the ng-app from the html and then inside of the constructor of the Module you need to call ngDoBootstrap() to bootstrap angular manually. 首先,您需要从html中删除ng-app,然后在模块的构造函数内部,您需要调用ngDoBootstrap()来手动引导角度。

upgrade.bootstrap(document.body, ['app'], {strictDi: true}); upgrade.bootstrap(document.body,['app'],{strictDi:true}); will take care of bootstrapping your AngularJS application. 将负责引导你的AngularJS应用程序。

 export class AppModule { constructor(private ngRedux: NgRedux<any>, @Inject(DIGEST_MIDDLEWARE) digestMiddleware: any) { const reducer = combineReducers({ actions: actionsReducer, ...coreReducers }); const initialStete = { actions: actionsInitialState, ...coreState }; // digestMiddleware needs to be last, to support use in AngularJS 1.x const store = createStore(reducer, initialStete, applyMiddleware(thunk, logger, digestMiddleware) ); ngRedux.provideStore(store); } ngDoBootstrap() {} } platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => { const upgrade = platformRef.injector.get(UpgradeModule); upgrade.bootstrap(document.body, ['app'], {strictDi: true}); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM