10

以下起点

我们有一些 Angular 前端微服务,现在我们想将它们整合到一个应用程序中。我们正在尝试 Angular-Elements 方法,并且我们已经导出了一个产生巨大 JS 文件的微服务。

问题

当我们在“bring it together”项目中包含 angular 元素时,只是“router-outlet”标签在 dom 中,但没有呈现 html。

代码

自定义元素:

app.module.ts:

        import {NgModule, Injector} from '@angular/core';

    import {AppRoutingModule} from './app-routing.module';
    import {AppComponent} from './app.component';

    import {PhoneModule} from "./features/phone/phone.module";
    import {createCustomElement} from '@angular/elements';
    import {Router} from '@angular/router';

    @NgModule({
      declarations: [
        AppComponent,
      ],
      imports: [
        AppRoutingModule,
        PhoneModule,
      ],
      entryComponents: [AppComponent],
    })
    export class AppModule {

      constructor(private injector: Injector, private router: Router) {
        this.router = router;
      }

      ngOnInit() {
        this.router.initialNavigation();
      }

      ngDoBootstrap() {
        const customElement = createCustomElement(AppComponent, {injector: this.injector});
        customElements.define('phone-contracts', customElement);
      }
    }

应用程序路由.module.ts:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PhoneListComponent } from './features/phone/phone-list/phone-list.component';
import {PhoneContractDetailsComponent} from "./features/phone/phone-contract-details/phone-contract-details.component";

const routes: Routes = [
    { path: '', redirectTo: 'phone-list', pathMatch: 'full' },
    { path: '', component: PhoneListComponent,},
    { path: 'phone-list/:id', component: PhoneContractDetailsComponent },
    { path: '**', redirectTo: '/phone-list', pathMatch: 'full'}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

带上它的项目: 这些来自应该处理这个特定微服务的组件

电话合同.component.html:

<p>
  phone-contracts works!
</p>

<phone-contracts>
  <app-phone-list>
  </app-phone-list>
</phone-contracts>

app.module.ts:

import {CUSTOM_ELEMENTS_SCHEMA, NgModule} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { FooComponent } from './foo/foo.component';
import { HttpClientModule } from '@angular/common/http';
import { CookieService } from 'ngx-cookie-service';
import { AppRoutingModule } from './app-routing.module';
import { AppService } from './app.service';
import { PhoneContractsComponent } from './phone-contracts/phone-contracts.component';
import { DeviceListComponent } from './device-list/device-list.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    LoginComponent,
    FooComponent,
    PhoneContractsComponent,
    DeviceListComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    HttpClientModule],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  providers: [CookieService,AppService],
  bootstrap: [AppComponent]
})
export class AppModule { }

可能重要的东西?

  • 我们将其用作我们的“构建器”:“@angular-devkit/build-angular:browser”,

  • 我们认为问题在于自定义元素无法解析路由,因此什么也不显示,这可能是真的吗?

4

1 回答 1

4

这是您的路线列表应如下所示:

const routes: Routes = [
    { 
        path: 'phone-list',
        component: PhoneListComponent
    },
    { 
        path: 'phone-list/:id',
        component: PhoneContractDetailsComponent 
    },
    { 
        path: '**', 
        redirectTo: 'phone-list', 
        pathMatch: 'full'
    }
];

除此之外,您实际上还没有指定您的 router-outlet 元素存在的位置。如果它显示为 html,那么您可能没有关闭它的父标签和/或它没有在 Angular 组件上使用。

于 2019-04-16T22:28:10.830 回答