"-6ren"> "-以下起点: 我们有一些 Angular 前端微服务,现在我们想将它们整合到一个应用程序中。我们正在尝试 Angular-Elements 方法,我们已经导出了一个产生巨大 JS 文件的微服务。 问题 -6ren">
gpt4 book ai didi

Angular Element 不解析另一个 Angular 项目中的路由(?),网站上只显示 ""

转载 作者:太空狗 更新时间:2023-10-29 17:27:54 24 4
gpt4 key购买 nike

以下起点:

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

问题

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

代码

自定义元素:

应用程序模块.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 { }

整合项目:这些来自应该处理此特定微服务的组件

电话契约(Contract).component.html:

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

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

应用程序模块.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”,

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

最佳答案

这是你的路由列表应该是这样的:

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

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

关于Angular Element 不解析另一个 Angular 项目中的路由(?),网站上只显示 "<router-outlet></router-outlet>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com