我在点击导航栏上的相关按钮时尝试刷新页面时遇到了同样的问题。
Angular 声明在同一个 URL 上的导航没有任何效果,因为应用程序的状态由它的当前 URL 表示。
有一个解决方法:onSameUrlNavigation
不幸的是,这个选项只允许守卫和解析器在导航到相同的 URL 时再次运行,所以如果你激活它,你会发现......什么都没有!
所以我用这个类创建了自己的解决方法:
/**
* Abstract class that allows derived components to get refreshed automatically on route change.
* The actual use case is : a page gets refreshed by navigating on the same URL and we want the rendered components to refresh
*/
export abstract class AutoRefreshingComponent implements OnInit, OnDestroy {
public routerEventsSubscription: Subscription;
protected router: Router;
constructor() {
this.router = AppInjector.get(Router);
}
/**
* Initialization behavior. Note that derived classes must not implement OnInit.
* Use initialize() on derived classes instead.
*/
ngOnInit() {
this.initialize();
this.routerEventsSubscription = this.router.events.filter(x => x instanceof NavigationEnd).subscribe(res => {
this.initialize();
});
}
/**
* Destruction behavior. Note that derived classes must not implement OnDestroy.
* Use destroy() on derived classes instead.
*/
ngOnDestroy(): void {
this.routerEventsSubscription.unsubscribe();
this.destroy();
}
/**
* Function that allows derived components to define an initialization behavior
*/
abstract initialize(): void;
/**
* Function that allows derived components to define a destruction behavior
*/
abstract destroy(): void;
}
AppInjector 指的是这个:
import {Injector} from '@angular/core';
/**
* Allows for retrieving singletons using `AppInjector.get(MyService)` (whereas
* `ReflectiveInjector.resolveAndCreate(MyService)` would create a new instance
* of the service).
*/
export let AppInjector: Injector;
/**
* Helper to access the exported {@link AppInjector}, needed as ES6 modules export
* immutable bindings; see http://2ality.com/2015/07/es6-module-exports.html
*/
export function setAppInjector(injector: Injector) {
if (AppInjector) {
// Should not happen
console.error('Programming error: AppInjector was already set');
}
else {
AppInjector = injector;
}
}
在您的 AppModule 中:
import { setAppInjector } from './app.injector';
// ...
export class AppModule {
constructor(private injector: Injector) {
setAppInjector(injector);
}
}
然后让所有需要的组件扩展AutoRefreshingComponent。
在你的情况下:
@Component({
selector: 'app-user',
template: `
<input #box (keyup.enter)="onEnter(box.value)">
<div *ngIf="box.size > 0">
<app-order [value]="value"></app-order>
</div>`
})
export class UserComponent extends AutoRefreshingComponent {
value = '';
onEnter(value: string) {
this.value = value;
}
constructor() {
}
initialize() {
// Reset your box value
}
destroy() {
}
}
这对于您想要实现的目标可能有点过头了,但每次您想在相同的 URL 导航上刷新组件时它都会很有用。
如果这有帮助,请告诉我。