简体   繁体   English

如何使用 CanActivate routerguard 来控制没有路径的组件的加载

[英]How to use CanActivate routerguard to control the loading of a component without a path

I understand router guards are used to control whether or not a component loads based on some condition.我了解路由器防护用于控制是否根据某些条件加载组件。 However, they typically are used with routes, which require paths.但是,它们通常与需要路径的路线一起使用。

My use case is: I have an Angular Fire website that allows users to log in. I want a component to be loaded only once the firebase server has started, because the component involves the auth service (the component being loaded after the server prevents "Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp()").我的用例是:我有一个允许用户登录的 Angular Fire 网站。我希望仅在 firebase 服务器启动后才加载组件,因为该组件涉及身份验证服务(在服务器阻止“错误:没有 Firebase 应用程序“[DEFAULT]”已创建 - 调用 Firebase App.initializeApp()")。 The issue is, this component isn't a dedicated view in the web app, it's just a smaller sidemenu used in a different, larger component.问题是,此组件不是 web 应用程序中的专用视图,它只是用于不同的更大组件的较小侧菜单。 So, this component doesn't have it's own path (ie URL).所以,这个组件没有它自己的路径(即 URL)。

I'm only familiar with router guards being used within the routes declaration.我只熟悉在路由声明中使用的路由器防护。 So, I tried the below (without specifying a path), and it didn't work (Error: Invalid configuration of route '': routes must have either a path or a matcher specified).所以,我尝试了以下(没有指定路径),但它没有工作(错误:路由''的配置无效:路由必须指定路径或匹配器)。 How would one handle this?一个人会如何处理这个? I'm a beginner to Angular so I'm unsure as to the best practices.我是 Angular 的初学者,所以我不确定最佳实践。

const routes: Routes = [
  { component: SidemenuComponent, canActivate: [AngularFireAuthGuard]},
];

You can combine multiple components with a specific URL.您可以将多个组件与特定的 URL 组合。 For the restricted path just add an empty component or other information that the menu is not available.对于受限路径,只需添加一个空组件或菜单不可用的其他信息。
App module routes:应用模块路由:

const routes: Routes = [
    {
        path: "load",
        canActivate: [ServerDataGuard],
        children: [
            {
                path: "",
                outlet: "primary",
                component: CommonComponent,
            },
            {
                path: "",
                outlet: "data",
                component: ServerDataComponent
            },
        ],
    },
    {
        path: "",
        children: [
            {
                path: "",
                outlet: "primary",
                component: CommonComponent,
            },
            {
                path: "",
                outlet: "data",
                component: EmptyComponent,
            },
        ],
    },
];

Server data guard (for example, filtering by id = 10):服务器数据保护(例如,按 id = 10 过滤):

public canActivate(
    _route: ActivatedRouteSnapshot,
    _state: RouterStateSnapshot,
) {
    const params = _route.queryParams;
    return params["id"] == 10;
}

And some HTML sample (outlet 'primary' is a default name):还有一些 HTML 示例(出口“主”是默认名称):

<table>
    <tr>
        <th>Common</th>
        <th>Server data</th>
    </tr>
    <tr>
        <td><router-outlet></router-outlet></td>
        <td><router-outlet name="data"></router-outlet></td>
    </tr>
</table>
<br />

<a routerLink="/load" [queryParams]="{ id: 10 }">Try load data with id = 10</a><br>
<a routerLink="/load">Try load data without id</a><br>
<a routerLink="/">Back</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM