简体   繁体   English

Angular 组件降级后不会在 angularjs 应用程序中呈现

[英]Angular component downgraded not render in angularjs application

I'm trying to downgrade my Angular component to make it use in AngularJS app.我正在尝试降级我的 Angular 组件以使其在 AngularJS 应用程序中使用。

I looked at the documentation in Angular, but the template don't render in the page.我查看了 Angular 中的文档,但模板未在页面中呈现。

assistance.component.ts :协助.component.ts:

import {Component} from "@angular/core";
import {downgradeComponent} from "@angular/upgrade/static";
declare var angular: angular.IAngularStatic;

@Component({
    selector: 'assistance',
    template: '<h1>Assistance</h1>',
})
export class AssistanceComponent {

    constructor() {
    }
}

angular.module('AssistanceModule', []).directive('assistance', downgradeComponent({component: AssistanceComponent}) as angular.IDirectiveFactory);

main.ts (for the Angular App) : main.ts(用于 Angular 应用):

@NgModule({
    imports: [
        BrowserModule,
        UpgradeModule,
        HttpClientModule,
        FormsModule,
        CommonModule
    ],
    declarations: [
        AssistanceComponent
    ],
    entryComponents: [
        AssistanceComponent
    ],
})
export class AppModule {
    ngDoBootstrap() {
    }
}

// Bootstrap using the UpgradeModule
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
    console.log("Bootstrapping in Hybrid mode with Angular & AngularJS");

    const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
    upgrade.bootstrap(document.body, [sampleAppModuleAngularJS.name]);
});

app.ts (for the angularjs app) : app.ts(用于 angularjs 应用程序):

export const sampleAppModuleAngularJS = angular.module('AngularJS',
        [
            'ui.router',
            'ngMessages',
            'ngSanitize',
             ...
            'AssistanceModule'
        ]).config(function($stateProvider, $urlRouterProvider) {
        $urlRouterProvider.otherwise('/home');

        $stateProvider
            .state('homeComponent', {
                url: '/home',
                template: '<home></home>'
            })
            .state('AssistanceComponent', {
                url: '/assistance',
                template: '<assistance></assistance>',
            })
});

When I go to the URL "/assistance" with $state.go, <assistance> </assistance> is show in the console, but not the angular template.当我使用 $state.go 访问 URL“/assistance”时, <assistance> </assistance>显示在控制台中,但不显示角度模板。

If I integrate the AssistanceComponent in bootstrap, like that :如果我将 AssistanceComponent 集成到引导程序中,如下所示:

...
declarations: [
        AssistanceComponent
    ],
    entryComponents: [
        AssistanceComponent
    ],
    bootstrap: [
        AssistanceComponent
    ]
...

And add <assistance></assistance> in index.html, the template is good, but not like I want (integrate in the main content).并在index.html中添加<assistance></assistance> ,模板不错,但不是我想要的(集成到主要内容中)。

I don't know what i am doing wrong...我不知道我在做什么错...

I found the problem.我发现了问题。

The version of angular/core and angular/upgrade were not the same. angular/core 和 angular/upgrade 的版本不一样。

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

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