【问题标题】:Angular Element not resolving routes(?) in another angular project, instead only "<router-outlet></router-outlet>" is shown on WebsiteAngular Element 未在另一个 Angular 项目中解析路由(?),而网站上仅显示“<router-outlet></router-outlet>”
【发布时间】:2019-04-12 11:33: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);
      }
    }

app-routing.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”,

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

【问题讨论】:

  • 我也有类似的问题。我的自定义元素中没有发生路由。非常简单的元素。单个自定义元素中只有两个组件。你知道如何在自定义元素中启用路由吗?
  • 我无法想象它们是如何组合在一起的。你把&lt;router-outlet&gt;放在哪里?我猜它在 AppComponent 本身内部,因为它是一个自定义组件,所以你遇到了问题。您是否介意在 stackblitz 中创建一个可以重现该问题的示例。
  • 我敢打赌,自定义元素的设计不能很好地与路由配合使用。和@trungk18 一样,我很难绕开它。
  • @mehlichmeyer Maximillion Bartango 提供的解决方案对您有用吗?
  • @trungk18 我也面临同样的问题,有人找到解决方案了吗?那太好了

标签: angular web-component angular-elements


【解决方案1】:

您的路线列表应该是这样的:

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

除此之外,您实际上还没有指定您的路由器出口元素存在的位置。如果它显示为 html,那么您可能没有关闭它的父标签和/或它没有被用于角度组件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2021-09-27
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2023-02-15
    相关资源
    最近更新 更多