我刚刚使用 downgradeModule 设置了一个混合 AngularJS / Angular 5 应用程序。我已经将一个非常小的组件从 AngularJS 转换为 Angular,但它从未被创建。我已将 console.logs 放在整个组件中,以查看是否调用了某些位而其他位不被调用。文件已加载,但组件从未加载。
我已经阅读了数百个教程,但我一定遗漏了一些东西。
请注意,我在转换组件方面已经走了这么远,意识到它没有被创建,然后停止移植其余部分。因此,为什么driverImage
当前不在转换后的组件上。
这是一个带有测试组件的 stackblitz,显示它不工作https://angularjs-q1vrpe.stackblitz.io/
引导程序
import * as angular from "angular";
import { downgradeModule } from "@angular/upgrade/static";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { StaticProvider } from "@angular/core";
import { MainAngularModule } from "./app.module";
import "./index.module";
const bootstrapFn = (extraProviders: StaticProvider[]) => {
console.log("1"); <--- never output!
const platformRef = platformBrowserDynamic(extraProviders);
return platformRef.bootstrapModule(MainAngularModule);
};
const downgradedModule = downgradeModule(bootstrapFn);
angular.module('angularJsModule', ['myApp', downgradedModule]);
angular.bootstrap(document, ['angularJsModule']);
App.Module (MainAngularModule)
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
@NgModule({
imports: [
BrowserModule,
UpgradeModule
],
declarations: [Ng2TestComponent, DriverImageComponent],
entryComponents: [Ng2TestComponent, DriverImageComponent]
})
export class MainAngularModule {
// Empty placeholder method to satisfy the `Compiler`.
ngDoBootstrap() { }
}
index.module(为简洁起见,删除了大部分依赖项)
angular
.module("myApp", [
"constants",
"ngMaterial",
"ngSanitize", ... ]);
新转换的组件:
import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { IDriver } from "../interfaces/IDriver";
console.log("here"); // --> This is hit
@Component({
selector: "driver-image",
template: `<div class="driver-image" ng-style="{'background-image' : 'url('+$ctrl.driverImage+')'}"></div>`
})
export class DriverImageComponent implements OnInit, OnChanges {
@Input() driver: IDriver;
@Input() small: boolean;
constructor() {
console.log("1"); // --- Not hit
}
ngOnInit() {
console.log("ONINITNINTT"); // --> Not hit
}
ngOnChanges() { }
}
相关模块.ts
import { downgradeComponent } from "@angular/upgrade/static";
...
.component("driverImage", downgradeComponent({ component: DriverImageComponent }))