【问题标题】:Angular: Setup routes depending on service method callAngular:根据服务方法调用设置路由
【发布时间】:2017-12-13 13:05:25
【问题描述】:

我通过@NgModule 设置了路由配置。而且我有一项服务,可以根据某些条件确定应向用户显示应用程序的哪些部分。我需要调用该服务并根据返回的值设置路由。

问题:路由配置是在注解中设置的,我不知道如何在这样的设置中调用服务。

这里更具体地说是我要增强的示例配置。

我当前的路由设置:

const appRoutes: Routes = [
    {
        path: '',
        redirectTo: 'first-route',
        pathMatch: 'full'
    },
    {
        path: 'first-route',
        component: FirstComponent,
        pathMatch: 'full'
    },
    {
        path: 'second-route',
        component: SecondComponent,
        pathMatch: 'full'
    },
    ...
];

@NgModule({
    imports: [RouterModule.forChild(appRoutes)],
    exports: [RouterModule]
})
export class MyRoutingModule {
}

应该改变路由设置的服务:

@Injectable()
export class MyService {
    getAccessibleRoutes(): Observable<string[]> {...}
}

问题:如何拨打服务电话并更改路线?

注意:我也查看了"Dynamically adding routes in Angular""How we can add new routes dynamically into RouterModule(@NgModule imports)",但我还没有找到明确的答案。

【问题讨论】:

    标签: javascript angular typescript angular-ui-router url-routing


    【解决方案1】:

    如果我正确理解了您的问题,我认为您可能可以考虑使用路由守卫来达到您的目标。我建议您使用守卫功能来指定访问路由的条件,而不是更改路由列表。

    有关路由守卫的更多信息,请查看此链接:

    https://codecraft.tv/courses/angular/routing/router-guards/

    希望对你有帮助。

    import { Injectable } from '@angular/core';
    import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    import { YourSecurityService } from './your-security.service';
    
    @Injectable()
    export class YourRouteGuardService implements CanActivate {
    
        constructor(
            private router: Router, 
            private yourSecurityService: YourSecurityService) {
        }
    
        canActivate(
            route: ActivatedRouteSnapshot, 
            state: RouterStateSnapshot): boolean {
    
            console.log(state.url); // HERE YOU CAN GET REQUESTED ROUTE
    
            if (this.yourSecurityService.checkIfUserHaveAccess())
                return true;
    
            this.router.navigate(['your-route-to-redirect']);
            return false;
        }
     }
    

    接下来你应该对你的路线施加警惕:

    const appRoutes: Routes = [
        {
            path: 'someroute',
            component: RouteComponent,
            canActivate: [YourRouteGuardService]
        },
        ...
    ]
    

    【讨论】:

    • 我如何设置redirectTo 与路由器防护?例如,如果我知道用户无法访问 first-route,我想将重定向更改为 second-route
    • 请检查更新的答案。在这里,您将找到一个示例,说明如果您的激活条件不满足,如何重定向到另一条守卫路线
    • @OleksandrShpota 在您的问题中,您的初始方法似乎是根据应用内逻辑动态更改路由配置。我同意 Taras 的观点,更正确的方法是预先定义整个路由器配置,并使用 Route Guards 根据应用内数据限制对每条路由的可访问性。有关更多信息,请参阅 Angular 文档angular.io/guide/…
    • @TarasIlyin 但我如何知道canActivate 最初请求的是哪条路线?
    • @OleksandrShpota:我已经为您更新了答案。您可以从传入的 canActivate 参数中获取请求的路由。请查看角度 CanActivate 接口的 api 参考以获取更多详细信息。让我知道是否可以为您提供更多帮助。
    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    相关资源
    最近更新 更多