angular 5.2 onSameUrlNavigation无法正常工作

hel*_*ldt 14 angular angular5

我正试图在我的角度5.2应用程序中重新加载我的导航而没有任何成功.如果参数不变,角度路由器将忽略我的导航.

我如何导航:

this.router.navigate(['/search', buildParamsFromSearchCriteria(criteria)]);
Run Code Online (Sandbox Code Playgroud)

导航到:

/search;pageSize=20;page=1;orderBy=price;sortDirection=ASCENDING
Run Code Online (Sandbox Code Playgroud)

我的模块配置:

imports: [RouterModule.forRoot(appRoutes, { preloadingStrategy: PreloadAllModules, enableTracing: false, onSameUrlNavigation: 'reload' })],
Run Code Online (Sandbox Code Playgroud)

Twi*_*her 0

当我点击导航栏上的关联按钮尝试刷新页面时,我遇到了同样的问题。

如评论中所述,onSameUrlNavigation仅运行防护程序和解析器,但不重新初始化组件。有趣的是它还会触发导航。

所以我创建了一个对事件做出反应的抽象类NavigationEnd

/**
 * 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;

}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在应用程序模块中:

import { setAppInjector } from './app.injector';

// ...

export class AppModule {
  constructor(private injector: Injector) {
    setAppInjector(injector);
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我让所有需要的组件扩展AutoRefreshingComponent并实现所需的功能。

希望这个迟来的答案有所帮助。