繁体   English   中英

this.router.navigate() 不导航到 URL

[英]this.router.navigate() doesn't navigate to the URL

我在 Angular 中有一个分页演示应用程序。 我希望能够导航到像http://localhost:4200/page/:number这样的 URL。 URL 在浏览器的 URL 地址栏中似乎正在更改,但我必须在浏览器的 URL 地址栏中按 Enter 键才能实际导航到 URL 并更改 HTML 表上的数据。

Stackblitz 在这里: https : HttpErrorResponseHttpErrorResponse不是问题的一部分,但我不知道如何在 Stackblitz 中解决这个问题)。 这是我的代码:

page.component.html:

<span *ngFor="let x of fakeArray; let index = index">
  <button type="button" class="btn btn-primary" (click)="goToPage(index)">{{ index + 1 }}</button>
</span>

<br><br>

<table class="table">
  <thead>
    <tr>
      <th>Alue-ID</th>
      <th>Kunta</th>
      <th>Lääni</th>
      <th>Maakunta</th>
      <th>Seutukunta</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let municipality of municipalities">
      <td>{{ municipality.alue_id }}</td>
      <td>{{ municipality.kunta }}</td>
      <td>{{ municipality.laani }}</td>
      <td>{{ municipality.maakunta }}</td>
      <td>{{ municipality.seutukunta }}</td>
    </tr>
  </tbody>
</table>

<span *ngFor="let x of fakeArray; let index = index">
  <button type="button" class="btn btn-primary" (click)="goToPage(index)">{{ index + 1 }}</button>
</span>

<br><br>

page.component.ts:

import { Component, OnInit } from '@angular/core';
import { MunicipalitiesService } from '../municipalities.service';
import { municipality } from '../municipality.interface';
import { ActivatedRoute, Router } from '@angular/router';

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

  municipalities: municipality[] = [];
  pagenumber: number;
  fakeArray: any;

  constructor(
    private service: MunicipalitiesService, 
    private route: ActivatedRoute,
    private router: Router) { }

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.pagenumber = +params.number;
    })

    this.service.getOneHundredRecords(this.pagenumber).then((data: municipality[]) => {
      this.municipalities = data;
    });

    this.service.getPageCount().then((pageCount: number) => {
      this.fakeArray = new Array(pageCount);
    })
  }

  goToPage = (index: number) => {
    this.router.navigate([`/page/${index}`]);
  }

}

市政当局.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { municipality } from './municipality.interface';

@Injectable()
export class MunicipalitiesService {

  constructor(private http: HttpClient) { }

  getOneHundredRecords = (page: number) => {
    return new Promise((resolve, reject) => {
      this.http.get("/assets/data.json").subscribe((data: municipality[]) => {
        let municipalities = data;
        let index = 0;
        let toBeReturned: municipality[] = [];

        for (let municipality of municipalities) {
          if (index >= (page * 100) && index < (page + 1) * 100) {
            toBeReturned.push(municipality);
          }
          index++;
        }
        resolve(toBeReturned)
      })
    })
  }

  getPageCount = () => {
    return new Promise((resolve, reject) =>  {
      this.http.get("/assets/data.json").subscribe((data: municipality[]) => {
        let municipalities = data;
        let index = 0;

        for (let municipality of municipalities) {
          index++;
        }

        let pageCount = Math.ceil(index / 100);
        resolve(pageCount)
      })
    })
  }

}

当您想从当前所在的路由导航到同一路由时,Angular 路由器有一项策略会阻止您。您可以像这样使用 RouteReuseStrategy:

page.component.ts:

***

constructor() {
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
}

***

这将允许您的组件在您导航到她(从她那里)后刷新。

希望这可以帮助!

暂无
暂无

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

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