gpt4 book ai didi

javascript - 带 2 个路由器 socket 的 Angular 6

转载 作者:行者123 更新时间:2023-11-29 17:44:39 25 4
gpt4 key购买 nike

如何确保至少有 2 个路由器 socket 并在路由级别管理它们?可以给我链接一个工作的 jsfillde 或 stackblitz 或类似的吗?

编辑重新打开问题

应用组件 HTML

<main [@fadeInAnimation]="o.isActivated ? o.activatedRoute : ''">
<router-outlet #o="outlet" name="main"></router-outlet>
<router-outlet #o="outlet" name="second"></router-outlet>
</main>

应用模块

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

// components
import { AppComponent } from './app.component';
import { HomeComponent } from './components/home/home.component';

// models
import { Permissions } from '../app/models/permissions';

// guards
import { CanAccess } from '../app/guards/canaccess';
import { OtherComponent } from './components/other/other.component';
import { PageNotFoundComponent } from './components/page-not-found/page-not-found.component';

const permissions: Permissions = new Permissions();

const appRoute: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, data: { permission: permissions }, canActivate: [CanAccess], outlet: 'main' },
{ path: 'other', component: OtherComponent, data: { permission: permissions }, canActivate: [CanAccess], outlet: 'second' },
{ path: 'pageNotFound', component: PageNotFoundComponent, data: { permission: permissions }, canActivate: [CanAccess], outlet: 'main' },
{ path: '**', redirectTo: 'pageNotFound', pathMatch: 'full' },
];

export function appConfigFactory() {
try {
return () => true;
} catch (e) {
console.log(`catch load: ${e}`);
}
}

@NgModule({
declarations: [
AppComponent,
HomeComponent,
OtherComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
RouterModule.forRoot(appRoute)
],
providers: [
CanAccess,
{
provide: APP_INITIALIZER,
useFactory: appConfigFactory,
deps: [],
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }

错误

错误错误:未捕获( promise ):错误:无法匹配任何路由。网址段:“家”错误:无法匹配任何路由。 URL 段:'home'

可以在编辑器上查看 https://stackblitz.com/edit/angular-ghusfs

谢谢

最佳答案

你可以定义父子组件使用多个router-outlets像这样。

{
path: 'shop', component: ParentShopComponent,
children : [{
path: 'hello', component: ChildShopComponent
}]
}

你的第一个<router-outlet>将在应用程序组件中,在 ParentShopComponent 中排名第二其余组件可以借给子级或父级。

但是,如果您的关系是 child parent ,那么请检查一下 Named Router Outlets

例子

这是主要的路由器输导出 这些是命名的

<div class="columns">
<md-card>
<router-outlet name="list"></router-outlet>
</md-card>
<md-card>
<router-outlet name="bio"></router-outlet>
</md-card>
</div>

下面是你如何使用它们

{ path: 'speakersList', component: SpeakersListComponent, outlet: 'list' }

关于javascript - 带 2 个路由器 socket 的 Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50854312/

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