带有参数“无法匹配任何路线"的角嵌套路线

编程入门 行业动态 更新时间:2024-10-07 10:24:57
本文介绍了带有参数“无法匹配任何路线"的角嵌套路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我正在研究的3条路线:

Here are the 3 routes I'm working on:

  • game/:id
  • 游戏/:id/定价
  • 游戏/:id/历史记录

我希望 game/:id 作为父视图,并容纳一个< router-outlet> 供孩子们在其中进行渲染.但是,我一直收到此错误:无法匹配任何路由.网址段:游戏/1/历史记录".

I'd like the game/:id to be the parent view, and house a <router-outlet> for the children to render inside of it. However, I keep getting this error: Cannot match any routes. URL Segment: 'game/1/history'.

感谢您的帮助.

import { NgModule } from "@angular/core"; import { RouterModule, Routes } from "@angular/router"; import { CommonModule } from "@angular/common"; import { GameComponent } from "./gameponent"; import { GamePricingComponent } from './game-pricingponent'; import { GameHistoryComponent } from './game-historyponent'; const routes: Routes = [ { path: 'game/:id', component: GameComponent, children: [ { path: 'pricing', component: GamePricingComponent, outlet: 'game' }, { path: 'history', component: GameHistoryComponent, outlet: 'game' } ]}, ]; @NgModule({ imports: [ RouterModule.forRoot(routes), CommonModule ], declarations: [ GameComponent, GamePricingComponent, GameHistoryComponent ], exports: [ RouterModule ] }) export class RoutingModule {}

GameComponent:

game/:id 路线是父视图,它在内部为孩子提供了< router-outlet name ="game"> (定价和历史记录).

GameComponent:

The game/:id route is the parent view, which has a <router-outlet name="game"> inside of it for the children (pricing & history) to render into.

import { Component } from "@angular/core"; @Component({ template: ` <div>Game stuff</div> <router-outlet name="game"></router-outlet> ` }) export class GameComponent {}

RootModule

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './appponent'; import { RoutingModule } from "./routing.module"; @NgModule({ imports: [ BrowserModule, RoutingModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export default class AppModule { }

AppComponent

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

推荐答案

一段时间后,我能够通过删除< router-outlet> 的命名来解决此问题.事实证明,单个子路由器出口正常工作"-因此不需要命名...

After a (long) while, I was able to solve this by removing the naming for the <router-outlet>. Turns out, single child router outlets 'just work' - so naming isn't required...

但是,在撰写本文时,我仍然不知道如何在子路由中使用路由器出口命名.如果有人想在这里详细说明,我将不胜感激.(也许只有在同一级别有多个插座的情况下才能命名?)

However, as of writing this, I'm still unaware of how to use router outlet naming within child routes. If anyone wants to elaborate here, I'd be more than appreciative. (maybe naming only works if there's more than 1 outlet at the same level?)

更多推荐

带有参数“无法匹配任何路线"的角嵌套路线

本文发布于:2023-11-25 21:34:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路线   嵌套   参数   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!