简体   繁体   English

角嵌套组件布线

[英]Angular nested components routing

I'm trying to show nested components from different modules in angular and came up with idea to do so with auxiliary routing. 我试图以角度显示来自不同模块的嵌套组件,并提出了通过辅助布线实现此想法。 (Although i'm not sure that's the best way to do so, and any thoughts on that would be very appriciated) (尽管我不确定这是否是最好的方法,对此的任何想法都非常有用)

app.component.ts app.component.ts

import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<router-outlet></router-outlet>`
})
export class AppComponent {}

app.module.ts app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LandingModule} from './landing/landing.module';
@NgModule({
  declarations: [
    AppComponent
    ],
  imports: [
    BrowserModule,
    LandingModule,
    AppRoutingModule
    ],
  providers: [],
  bootstrap: [AppComponent]
 })
export class AppModule { }

app-routing.moudle.ts app-routing.moudle.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
    {path: '', redirectTo: '/start', pathMatch: 'full'},];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
 })
export class AppRoutingModule { }

landing.component.ts landing.component.ts

import { Component } from '@angular/core';
@Component({
    selector: 'app-landing',
    template: `<app-landing-header></app-landing-header>
               <router-outlet name="login"></router-outlet>
               <app-landing-footer></app-landing-footer>`
})
export class LandingComponent {
}

landing.module.ts landing.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule} from '@angular/router';
import { LandingComponent } from './landing.component';
import { LandingHeaderComponent } from './landing-header/landing-header.component';    
import { LoginComponent } from './login/login.component';    
const routes: Routes = [
    {path: 'start', component: LandingComponent,
        children: [
            {   path: 'login',
                outlet: 'login',
                component: LoginComponent}
        ]},
]

@NgModule({
  imports: [
    CommonModule,
      RouterModule.forChild(routes)
  ],
  declarations: [LandingComponent,
      LandingHeaderComponent,          
      LoginComponent,
      ],
    exports: [
        RouterModule
    ]
})
export class LandingModule { }

When navigating to /start it shows corresponding component and landing-header working. 导航到/ start时,它显示相应的组件和着陆页眉的工作情况。 But when navigating to /start(login:login) I get error 但是导航到/ start(login:login)时出现错误

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. 错误错误:未捕获(承诺):错误:无法匹配任何路由。 URL Segment: 'login' 网址段:“登录”

I think that you have the same issues as https://github.com/angular/angular/issues/18271 我认为您遇到的问题与https://github.com/angular/angular/issues/18271

According to that thread, named outlets doesn't work properly as child routes. 根据该线程,命名的网点不能作为子路由正常工作。

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

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