簡體   English   中英

如何在不重新加載整個頁面的情況下重新加載 Angular 中的組件

[英]How to reload a component in Angular without reloading the whole page

我想重新加載路線但不重新加載整個頁面。 這不是我想要的:

window.location.reload();

我只想重新加載組件而不是頁面。 這是我試過的:

  1. Angular:更改路線無需重載
  2. Angular:在同一 URL 導航上重新獲取數據

我還嘗試了其他一些隨機技巧。 從這個 URL 嘗試以下代碼:

  1. Angular 基礎知識:刷新 Angular 組件而不重新加載相同的組件

    mySubscription: any; constructor() { this.router.routeReuseStrategy.shouldReuseRoute=function() { return false; } this.mySubscription= this.router.events.subscribe((event)=> { if(event instanceof NavigationEnd) { // Trick the Router into believing it's last link wasn't previously loaded this.router.navigated=false; } }) }

但由於其他原因,它不起作用。 我需要一個解決方案。 請提出其他建議。

skipLocationChange 選項對我有用,試試我的服務

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

@Injectable()
export class ReloadRouteService {

  constructor(
    private router: Router,
  ) { }

  redirectTo(url: string): void {
    // When skipLocationChange true, navigates without pushing a new state into history.
    this.router.navigateByUrl('/', {skipLocationChange: true}).then(() => {
        this.router.navigate([url]);
    });
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM