我有一個Angular 1應用程序,我遵循官方Angular教程(https://angular.io/docs/ts/latest/guide/upgrade.html)的指示來引導混合1 + 2應用程序。我擁有所有的代碼並運行應用程序,絕對沒有任何反應。它只是進入根網址,我只能看到index.html
中的任何內容的樣式和視圖。然而主要<div ng-view>
永遠不會通過$routeProvider
和app.ts
更新。至少,這似乎是發生了什麼事。有零錯誤。以下是我迄今爲止:使用降級組件引導的角度2 +角1混合應用程序不加載組件
main.ts(通過SystemJS加載)上述混合引導代碼
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { UpgradeModule } from '@angular/upgrade/static';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.body, ['MyA1App']);
});
一注 - 是據我可以告訴工作。如果我將Angular1應用程序名稱更改爲blahblah
,我會在瀏覽器中看到大量錯誤。此代碼可以找到app.ts
中定義的MyA1App
Angular1模塊,並且能夠引導它。
的index.html:
<div class="main-content" ng-view=""></div>
<!-- Configure SystemJS (bootstrap Angular 2 app) -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
<!-- Old Angular 1 bootstrap method -->
<!--<script>
angular.bootstrap(document.body, ['MyA1App']);
</script>-->
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static'; //To bootstrap a hybrid application, we need to import UpgradeModule in our AppModule, and override it's bootstrap method:
@NgModule({
imports: [BrowserModule, UpgradeModule ],
})
export class AppModule {
ngDoBootstrap() {}
}
import { downgradeComponent } from '@angular/upgrade/static';
angular.module('MyA1App', [])
.directive(
'contactList',
downgradeComponent({ component: ContactListComponent }) as angular.IDirectiveFactory
);
app.ts(從角1,其定義模塊和路由 - 這應該仍在玩和使用)
module MyApp {
'use strict';
export var App: ng.IModule = angular.module('MyA1App', ['ngRoute', 'ngResource', 'highcharts-ng', 'ui.bootstrap']);
// register work which needs to be performed on module loading
App.config([
'$routeProvider', '$httpProvider',
($routeProvider: ng.route.IRouteProvider, $httpProvider: ng.IHttpProvider) => {
$routeProvider
.when('/landing',
{
controller: MyApp.Controllers.LandingCtrl,
templateUrl: '/app/angular1x/partials/landing.html'
})
.when('/registration',
{
controller: MyApp.Controllers.RegistrationCtrl,
controllerAs: 'vm',
templateUrl: '/app/angular1x/partials/registration.html'
})
.otherwise({
redirectTo: '/landing'
});
}
]);
}
所以這裏的問題的概述:通過systemjs
- 的Bootstrap到Angular2
main.ts
稱爲index.html
- - 我可以把斷點Angular1控制器,着陸等,見(出現成功沒有錯誤)被定義和加載的控制器被擊中。但沒有其他事情發生!沒有錯誤或任何東西。
ng-view
指令永遠不會通過路由更新。在檢查主頁上它只是看起來像:<div class="main-content" ng-view=""></div>
隨時隨地- 如果我回去,並在
index.html
它的工作原理,而不是使用systemjs
加載A2和引導A1作爲混合使用原來的A1引導 - 沒有JS錯誤。
我在這裏做錯了什麼,以便原來的Angular1
應用似乎加載和引導就好了,但實際上並沒有做任何事情?
UPDATE:
我已經找到了問題的代碼要在在app.module.ts
如下:
import { downgradeComponent } from '@angular/upgrade/static';
angular.module('MyA1App', [])
.directive(
'contactList',
downgradeComponent({ component: ContactListComponent }) as angular.IDirectiveFactory
);
它不是路由(這是一個紅鯡魚)。如果我刪除上面的代碼,整個Angular1
應用程序將引導並加載。我在上面的代碼中做了什麼,可能會導致一切暫停加載,但不會創建任何JavaScript
錯誤?