【问题标题】:Angular routing doesn't open the desired default url角度路由未打开所需的默认 url
【发布时间】:2020-12-14 16:24:46
【问题描述】:

我正在使用 Angular 10 构建一个站点,但我对路由感到有些困惑。 应用路由配置如下:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const accountModule = () => import('./account/account.module').then(x => x.AccountModule);
const onboardingModule = () => import('./onboarding/onboarding.module').then(x => x.OnboardingModule);
const memberDashboardModule = () => import('./member-dashboard/member-dashboard.module').then(x => x.MemberDashboardModule);
const adminDashboardModule = () => import('./admin-dashboard/admin-dashboard.module').then(x => x.AdminDashboardModule);

const routes: Routes = [
  { path: 'account', loadChildren: accountModule },
  { path: 'onboarding', loadChildren: onboardingModule },
  { path: 'member-dashboard', loadChildren: memberDashboardModule },
  { path: 'admin-dashboard', loadChildren: adminDashboardModule },
  // otherwise redirect to home
  { path: '', redirectTo: 'member-dashboard', pathMatch: "full" }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, {enableTracing: true})],
  exports: [RouterModule]
})
export class AppRoutingModule { }

成员仪表板模块的路由如下:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { LayoutComponent } from './layout/layout.component';
import { UpdateProfileComponent } from './update-profile/update-profile.component'
import { ViewTeamsComponent } from './view-teams/view-teams.component'
import { CreateTeamComponent } from './create-team/create-team.component';
import { UpdateTeamComponent } from './update-team/update-team.component';
import { ViewNotificationsComponent } from './view-notifications/view-notifications.component';

const routes: Routes = [
    {
        path: '', component: LayoutComponent,
        children: [           
            { path: 'profile', component: UpdateProfileComponent },
            { path: 'notifications', component: ViewNotificationsComponent },
            { path: 'teams', component: ViewTeamsComponent },
            { path: 'teams/create-team', component: CreateTeamComponent },
            { path: 'teams/update-team', component: UpdateTeamComponent },
            { path: '', redirectTo: 'profile', pathMatch: 'full' }, 
        ]
    }
];

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

布局只是处理公共视觉/样式框架(标题、侧边栏等)内的子显示

<div class="full-screen-container">
    <router-outlet></router-outlet>
</div>

我希望用户浏览根目录(例如 http://localhost:8081)或任何不是 /member-dashboard、/admin-dashboard、/account 或 /onboarding 的内容,例如 (http://localhost: 8081/notExpectedPath) 被重定向到 http://localhost:8081/member-dashboard/profile。 但是,当我输入 http://localhost:8081 时,我会被重定向到 http://localhost:8081/profile。

你能帮我弄清楚吗?

【问题讨论】:

  • 这与延迟加载的模块无关。您需要一条“包罗万象”/“通配符”/“**”路线。请参阅此处:angular.io/guide/router-tutorial-toh#define-a-wildcard-route 了解操作方法。
  • 我更新了问题的标题以更好地反映问题。我希望我正确地解释了您的问题。

标签: angular typescript web routes


【解决方案1】:

发生的情况是您没有匹配“whatever”的规则,因此路由器正在检查您设置的所有规则,没有找到任何匹配项,并且正在激活最后一个。

您需要一条“包罗万象”的路线。有关说明,请参阅 here

要实现您的要求,您只需添加一个额外的路线作为数组中的绝对最后一条:

{ path: '**', redirectTo: '/member-dashboard' }

【讨论】:

  • 谢谢!我应该在应用程序路由中保留空路径规则还是只用通配符替换它?无论如何,我得说我遵循的本教程 angular.io/guide/lazy-loading-ngmodules 有点令人困惑,因为它不使用通配符路径并声明“前两条路径是通往 CustomersModule 和 OrdersModule 的路径。最后一项定义了默认路由。空路径匹配不匹配先前路径的所有内容。”,其中“最终条目”确实是空路径规则。
  • 指南是正确的,但是您混合并匹配了两个不同的指南:您使用 { path: '', redirectTo: 'profile', pathMatch: 'full' } 而不了解 pathMatch: 'full' 位。路由匹配(默认情况下)作为插入 url 的 part。所以路线“某事”也将匹配“某事/其他”。除非您使用pathMatch: 'full'。没有路径匹配的空 url 将完全像一个包罗万象(但我觉得它令人困惑,所以我总是使用 '**')。两者可以有不同的用途:空 url 可以用作默认路由,而 ** 用于经典的“未找到”页面。
  • 而且,如果答案被接受,您应该将其标记为已接受的答案(如果有价值,请点赞)。
  • 如果我在应用路由和成员仪表板路由中都使用通配符: // member-dashboard.routing.ts { path: '', component: LayoutComponent, children: [ { path: 'profile', component: UpdateProfileComponent }, { path: '', redirectTo: 'profile', pathMatch: "full" }, ] } // app-routing.ts { path: 'member-dashboard', loadChildren: memberDashboardModule }, { path: '', redirectTo: '/member-dashboard' } 还是不行:mywebsite.com 被重定向到 mywebsite.com/profile/member-dashboard/profile。
  • 看起来 { path: '**', redirectTo: '/member-dashboard' } 将继续处理 member-dashboard 模块中定义的路由并将它们与剩余的 url 段匹配(即"") 没有将“member-dashboard”附加到激活的 url。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多