简体   繁体   English

角儿童路线不变

[英]Angular Child Route not changing

I am designing a website with angular4, and have a component that inside uses the @angular/material sidenav. 我正在设计一个带有angular4的网站,并且有一个内部使用@ angular / material sidenav的组件。 Within that sidenav i have <router-outlet></router-outlet> . 在那边,我有<router-outlet></router-outlet>

my app.module has all the necessary imports and declarations, I have a app-routing module that contains the following code for the component: 我的app.module具有所有必要的导入和声明,我有一个app路由模块,其中包含该组件的以下代码:

const appRoutes: Routes = [
  { path: 'reg',
    component: RegComponent,
    children: [
        { path: ':info', component: RegInfoComponent },
        { path: ':ther', component: RegTherComponent }
    ]},
];

I have two span objects to open the sidenav: <span class="label label-info" [routerLink]="['/reg', 'info']" (click)=sidenav.open() style="cursor: pointer;">Info</span> 我有两个span对象来打开sidenav: <span class="label label-info" [routerLink]="['/reg', 'info']" (click)=sidenav.open() style="cursor: pointer;">Info</span>

and <span class="label label-info" [routerLink]="['/reg', 'ther']" (click)=sidenav.open() style="cursor: pointer;">Ther</span> <span class="label label-info" [routerLink]="['/reg', 'ther']" (click)=sidenav.open() style="cursor: pointer;">Ther</span>

The sidenav opens fine, but only loads the html template from RegInfoComponent, if i click the ther link, it still loads the html template from RegInfoComponent, but the URL does change. 该sidenav打开罚款,但只加载从RegInfoComponent HTML模板,如果我点击ther链接,它仍然加载从RegInfoComponent HTML模板,但URL不会改变。

At first i thought it was maybe because its inside the angular material sidenav, so i placed the <router-outlet></router-outlet> below the link, and it still wouldnt change. 起初,我认为可能是因为它位于有角度的材质sidenav内,所以我将<router-outlet></router-outlet>放置在链接下方,并且仍然不会更改。

I'm not sure what i've done wrong that the router-outlet is not displaying the correct child information, hoping somebody can help with this? 我不确定我做错了什么,即路由器插座未显示正确的子信息,希望有人可以提供帮助吗? Thanks. 谢谢。

Update 更新

The code for RegInfoComponent: RegInfoComponent的代码:

import { ActivatedRoute, Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-reg-info',
  templateUrl: './reg-info.component.html',
  styleUrls: ['./reg-info.component.css']
})
export class RegInfoComponent implements OnInit {

  constructor(route: ActivatedRoute,
              router: Router) { }

  ngOnInit() {
  }

}

and the code for the RegTherComponent 以及RegTherComponent的代码

import { ActivatedRoute, Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-reg-ther',
  templateUrl: './reg-ther.component.html',
  styleUrls: ['./reg-ther.component.css']
})
export class RegTherComponent implements OnInit {

  constructor(route: ActivatedRoute,
              router: Router) { }

  ngOnInit() {
  }

}

I think your router config is incorrect 我认为您的路由器配置不正确

you are using variable segments with :info and :ther so the first one ( :info ) will always match first (and the RegInfoComponent gets instantiated), and therefore RegTherComponent will never be instantiated 您将变量段与:info:ther因此第一个( :info )将始终首先匹配(并且RegInfoComponent被实例化),因此RegTherComponent将永远不会被实例化

You need to do something like this 你需要做这样的事情

<span class="label label-info" [routerLink]="['/reg/info']" (click)=sidenav.open() style="cursor: pointer;">Info</span>
<span class="label label-info" [routerLink]="['/reg/ther']" (click)=sidenav.open() style="cursor: pointer;">Ther</span>

and then the router config should be 然后路由器配置应该是

const appRoutes: Routes = [
  { path: 'reg',
    component: RegComponent,
    children: [
        { path: 'info', component: RegInfoComponent },
        { path: 'ther', component: RegTherComponent }
    ]},
];

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

相关问题 Angular-引导程序:从控制器更改状态时,使用Angular UI-route的页面导航(“下一步”和“后退”按钮)不起作用 - Angular - Bootstrap: Page navigation (Next and back button) using Angular UI-route not working when changing the state from controller Angular2:导航栏在将其放置在单个视图而不是app.component.ts中时尝试路由到不存在的子视图的问题 - Angular2: Issues with navbar trying to route to non-existent child views when placing it in individual views instead of app.component.ts Bootstrap 4 颜色在 Angular 9 中没有变化 - Bootstrap 4 colors are not changing in Angular 9 使用angular-route根据路线更改切换类别名称 - toggling class name based on route changes using angular-route 路由重定向时如何刷新Angular控制器 - How to refresh Angular controllers when route redirection 角路由不适用于嵌套控制器 - angular-route not working with nested controllers Angular Bootstrap窗口模式开启器路线更改 - Angular Bootstrap Window Modal Opener Route Changes Angular 2路由在某些页面上禁用导航栏链接 - Angular 2 route disable navbar links on some pages 在 Angular 2 组件模板中更改嵌套? - Changing nesting within an Angular 2 component template? 更改箭头填充引导轮播 - angular - Changing arrow padding bootstrap carousel - angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM