简体   繁体   English

前往 route/id 不会导致模板更新(地址栏显示正确的 route/id)

[英]Going to route/id doesn't cause template update (address bar shows correct route/id)

I'm just learining angular so sorry if it turns out to be silly question.我只是在学习 angular 如果这是个愚蠢的问题,我很抱歉。 I have a basic app where I have left-side panel (sidenav material component) and content area.我有一个基本应用程序,其中有左侧面板(sidenav 材质组件)和内容区域。 In the left-side panel I have a list of machines (machine s Componenet) and when user clicks any of them, matching machine (machineComponent) should be loaded to content area.在左侧面板中,我有一个机器列表(机器组件),当用户单击其中任何一个时,匹配的机器(机器组件)应该加载到内容区域。 Content area contains named router-outlet ('main'), so I managed to get this to work.内容区域包含命名路由器出口('main'),所以我设法让它工作。 When the user clicks for the first time some machine in the left-side panel, it gets loaded - this is visibible both in address bar and in machine template displaying its id (taken from the route at init).当用户第一次单击左侧面板中的某台机器时,它会被加载 - 这在地址栏中和显示其 id 的机器模板中都可见(取自 init 时的路由)。 The weird issue I'm having is that when the user clicks another machine, address bar apparently goes there (machine/id changes) but the template still shows the initial id.我遇到的奇怪问题是,当用户单击另一台机器时,地址栏显然会出现(机器/id 更改),但模板仍然显示初始 id。 No matter how many more machines the user clicks, address bar always update but the template stays on the initial id.无论用户点击多少台机器,地址栏总是更新,但模板保持在初始 id 上。 I've debugged it by putting breakpoint at machine constructor, and it seems it's called only once (doens't get called when user clicks other machine)..我已经通过在机器构造函数中放置断点来调试它,它似乎只被调用一次(当用户点击其他机器时不会被调用)..

Routing:路由:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MachinesComponent } from './machines/machines.component';
import { MachineComponent } from './machine/machine.component';
import { LinePerformanceComponent } from './line-performance/line-performance.component';

const routes: Routes = [
  { path: 'machines', component: MachinesComponent},
  { path: 'lineperformance', component: LinePerformanceComponent, children: [
    { path: 'machine/:id', component: MachineComponent, outlet: 'main'}
  ]},
  { path: '', redirectTo: '/lineperformance', pathMatch: 'full'}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Machines template:机器模板:

<mat-nav-list>
  <a *ngFor="let machine of machines" class="nav-item" mat-list-item [routerLink]="['/lineperformance', { outlets: {
      main: ['machine', machine.id]
    }}]">
    <mat-icon [ngClass]="machine.state=='PR'?'green-icon':'red-icon'">stop_circle</mat-icon>  
    {{ machine.name}}
  </a>
</mat-nav-list>

Machine component:机器组件:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { Machine } from '../machine';

@Component({
  selector: 'app-machine',
  templateUrl: './machine.component.html',
  styleUrls: ['./machine.component.css']
})
export class MachineComponent implements OnInit {
  machine: Machine;
  id: number;
  name: string;
  state: string;

  constructor(
    private route: ActivatedRoute,
    private location: Location
  ) { }

  ngOnInit() {
    this.id = +this.route.snapshot.paramMap.get('id');
  }

}

Lineperformance template (sidenav and content area container): < Lineperformance 模板(sidenav 和内容区域容器):<

mat-sidenav-container>
    <mat-sidenav mode="side" [(opened)]="menuOpened">
      <app-machines></app-machines>
    </mat-sidenav>
    <mat-sidenav-content>
      <button [ngClass]="menuOpened?'menuToggle-expanded':'menuToggle-collapsed'" id="menuToggle" mat-mini-fab color="primary" (click)="menuOpened=!menuOpened">
        <mat-icon>menu</mat-icon>
      </button>
      <router-outlet name="main"></router-outlet>
    </mat-sidenav-content>
  </mat-sidenav-container>

I don't think any other file is significant for this issue, but if it is, you can find complete project here .我不认为任何其他文件对这个问题很重要,但如果是,你可以在这里找到完整的项目。

Sorry, can't update the question.抱歉,无法更新问题。 I simplified the code.我简化了代码。 Now machine component should load to default router-outlet, which is located in mat-sidenav-content.现在机器组件应该加载到位于 mat-sidenav-content 中的默认路由器出口。 This works for the very first time and not anymore.这是第一次,现在不再有效。 The second (and subsequent) item you choose from sidenav, address bar changes (correctly) but the machine template doesn't update.您从 sidenav 中选择的第二个(以及后续)项目,地址栏更改(正确),但机器模板未更新。 I debugged it and constructor in machine component is load only the first time, whereas it should load each time you click an item..我对其进行了调试,机器组件中的构造函数仅在第一次加载,而每次单击项目时都应加载..

Please check example site here .在此处查看示例站点。 When I add target="_blank" to routerLink, it works correctly, but it's not what I want.. It's driving me nuts..当我将 target="_blank" 添加到 routerLink 时,它可以正常工作,但这不是我想要的......它让我发疯......

Sidenav container code (in line-performance component called from app.component): Sidenav 容器代码(在从 app.component 调用的线路性能组件中):

<mat-sidenav-container>
    <mat-sidenav mode="side" [(opened)]="menuOpened" role="navigation">
      <app-machines></app-machines>
    </mat-sidenav>
    <mat-sidenav-content>
      <router-outlet></router-outlet>
    </mat-sidenav-content>
  </mat-sidenav-container>

Sidenav items (located in machines component): Sidenav 项目(位于机器组件中):

<mat-nav-list>
  <mat-list-item *ngFor="let machine of machines">
    <a routerLink="/machine/{{machine.id}}">
      <mat-icon [ngClass]="machine.state=='PR'?'green-icon':'red-icon'">stop_circle</mat-icon>  
      {{machine.name}}
    </a>
  </mat-list-item>
</mat-nav-list>

Routes:路线:

const routes: Routes = [
  { path: 'machines', component: MachinesComponent},
  { path: 'machine/:id', component: MachineComponent}
];

Machine template:机器模板:

<p>To jest strona o indeksie: {{ this.machine.id }}</p>

Whole code on github github上的完整代码

Anyone?任何人? I'm not even counting on answer why it doesn't work, anything that puts me in the right direction will be a godsend.我什至不指望回答为什么它不起作用,任何让我朝着正确方向前进的东西都是天赐之物。 It's very basic example, everything according to angular's know how files, it really should be working but for some weird reason it doesn't.. I tested it on many machines, in both debug and production environments, the behavior is always the same..这是一个非常基本的例子,根据 Angular 知道文件的所有内容,它确实应该工作,但由于某些奇怪的原因它没有。我在许多机器上测试它,在调试和生产环境中,行为总是相同的。 .

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

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