【问题标题】:Is it possible to use two routes in angular?是否可以在角度使用两条路线?
【发布时间】:2019-07-04 14:29:36
【问题描述】:

我使用 Angular7 做了一个 Angular 应用程序,我尝试使用两个“router-outlet”标签, 例如,我有两个组件“main”和“main2”,它们一半相同,它们在父组件中路由,我想在两个“子组件”中路由它们的差异,这可能吗?你有什么探索的方法吗?谢谢;)

【问题讨论】:

    标签: angular-routing angular-component-router angular-v7


    【解决方案1】:

    好吧,如果我非常了解您的问题。例如,您很有可能拥有登录名和仪表板

    const routes: Routes = [
        {
            path: '',
            pathMatch: 'prefix',
            redirectTo: 'auth/login'
        },
        {
            path: 'loading',
            component: LoadingComponent
        },
        {
            path: 'auth/login',
            component: LoginComponent
        },
        {
            path: 'auth/forgot-password',
            component: ForgotPasswordComponent
        },
        {
            path: 'auth/register',
            component: RegisterComponent
        },
        {
            path: 'auth/reset-password-temp',
            component: ResetPasswordComponent
        },
        {
            path: 'dashboard',
            component: AdminComponent,
            canActivate: [AuthGuard],
            children: [
                {
                    path: 'welcome',
                    component: WelcomeComponent
                },
                {
                    path: 'messages',
                    canActivate: [AuthGuard],
                    component: MessagesComponent
                },
                {
                    path: 'student-add',
                    component: StudentAddComponent
                },
                {
                    path: 'student-edit',
                    component: StudentEditComponent
                },
                {
                    path: 'student-view',
                    component: StudentViewComponent
                }
    
            ]
        }
    ];
    
    @NgModule({
        imports: [RouterModule.forRoot(routes, { useHash: true })],
        exports: [RouterModule],
        providers: []
    })
    export class RoutingModule {}
    

    导入每个组件并正确引用路径

    【讨论】:

    • 非常感谢,我在您之前找到了另一种方法,但您的解决方案对我来说很有趣,如何使用儿童部分?子组件将出现在哪里?有没有“router-children”标签之类的?
    • 不,只有一个 会启用它,它会出现在它被引用的地方
    【解决方案2】:

    对于和我有同样问题的人,我使用“main”组件和“auth”组件。 “main”组件包含“router-outlet”标签,我的“auth”组件包含我的登录视图。 在我的 app.componnt.html 中,我只使用了

    <app-auth *ngIf="!isAuth"></app-auth>
    <app-main *ngIf="isAuth"></app-main>

    使用该方法,任何想要访问特定路径的用户都不会被重定向到身份验证视图,并且会保留他想要访问的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多