[英]How to reload a component in Angular without reloading the whole page
我想重新加載路線但不重新加載整個頁面。 這不是我想要的:
window.location.reload();
我只想重新加載組件而不是頁面。 這是我試過的:
我還嘗試了其他一些隨機技巧。 從這個 URL 嘗試以下代碼:
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.