角度路由 - 区分新页面请求和不同模块的导航

时间:2017-09-20 14:50:38

标签: angular routing

在我的Angular应用程序中,我有一个仪表板组件,将在第一页加载时显示。在加载应用程序时,我想向用户显示一些启动他的提示。当他从另一个组件导航到仪表板时,我不想显示这些提示。

如何区分新页面请求和从其他页面导航到仪表板的用户?

1 个答案:

答案 0 :(得分:1)

我们通过将解析器放在out代码中来实现。它将在路由到达页面之前解析或执行。那时您可以检查用户是直接来自还是来自其他页面。并且根据你可以显示内容

@Injectable()
export class DashResolverService implements Resolve<any> {

  constructor(private router: Router) { }

  public resolve(route: ActivatedRouteSnapshot,
                 state: RouterStateSnapshot): Observable<any> | TitleResolverData {

    // this will give you weather you are coming from start or from another page
    if (!this.router.routeReuseStrategy.shouldAttach(route)) {
      return Observable.of(something);
    // you have data
    } else {
      return {
        title: 'Dashboard - ' + route.params.xxxx
      }
    };
  }

}

调整代码

您可以附上路线解析器

  resolve: {
    resolvedData: DashResolverService,
  }

这些是可用的方法

*/
export declare abstract class RouteReuseStrategy {
    /** Determines if this route (and its subtree) should be detached to be reused later */
    abstract shouldDetach(route: ActivatedRouteSnapshot): boolean;
    /** Stores the detached route */
    abstract store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void;
    /** Determines if this route (and its subtree) should be reattached */
    abstract shouldAttach(route: ActivatedRouteSnapshot): boolean;
    /** Retrieves the previously stored route */
    abstract retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null;
    /** Determines if a route should be reused */
    abstract shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean;
}