问题描述
我正在研究我的一个小项目,以了解有关Angular的更多信息,但是我真的无法弄清楚如何实现多层路由.
I'm working on a little project of mine in order to learn something more about Angular, but I really cannot figure out how to implement a multi-leveled routing.
我已阅读有关新版本的文档.路由器组件以及StackOverlfow上的其他一些主题(第一,第二,),但我找不到解决问题的方法.
I've read the documentation about the new release of the Router Component and also some other topics on StackOverlfow (first, second, third), but I cannot find a solution to my problem.
让我们考虑以下应用结构,而不考虑 Test 和 Test2 块.
Let's consider the following app structure, without considering the Test and Test2 blocks.
让我们考虑一下我的应用程序的组成部分:
And let's consider the components of my app as following:
main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { MyAppComponent } from './my-app/my-app.component';
import { APP_ROUTER_PROVIDERS } from './my-app/my-app.routes';
bootstrap(MyAppComponent, [ APP_ROUTER_PROVIDERS ])
.catch(err => console.error(err));
my-app.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'my-app',
template: '<router-outlet></router-outlet>',
directives: [ROUTER_DIRECTIVES],
})
export class MyAppComponent { }
my-app.routes.ts
import { provideRouter, RouterConfig } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { SignUpComponent } from './sign-up/sign-up.component';
import { AdminRoutes} from './admin/admin.routes';
export const routes: RouterConfig = [
{ path: '', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: 'sign-up', component: SignUpComponent },
...AdminRoutes,
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
admin.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'admin',
template: 'Hello I am ADMIN <br> <router-outlet></router-outlet>',
directives: [ROUTER_DIRECTIVES]
})
export class AdminComponent { }
admin.routes.ts
import { RouterConfig } from '@angular/router';
import { AdminComponent } from './admin.component';
import { MainPageComponent } from './main-page/main-page.component';
import { SettingsComponent } from './settings/settings.component';
export const AdminRoutes: RouterConfig = [
{
path: 'admin',
component: AdminComponent,
children: [
{ path: 'main-page', component: MainPageComponent },
{ path: 'settings', component: SettingsComponent },
]
}
];
main-page.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'main-page',
template: 'Hello I am MAIN PAGE!!!'
})
export class MainPageComponent { }
settings.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'settings',
template: 'Hello I am SETTINGS!!!'
})
export class SettingsComponent { }
当我尝试使用这样的配置时,任何操作都不再起作用,并且浏览器的控制台中充满了错误.
When I try to use such a configuration nothing works anymore and the browser's console is full of errors.
只有当我在 admin.routes.ts 中添加孩子[...] 时,才会发生这种情况,而我认为问题就出在这里.
This happens only when I add the children[...] in the admin.routes.ts, and I think the problems come in there.
- 能否请您提示我如何正确实施?
- 我想念什么吗?
- 有更好的方法吗?
在此先感谢您的帮助,希望本文所写内容对理解我的问题有所帮助!
Thank you in advance for your help and I hope what I've written in the post it's helpful to understand my issue!
推荐答案
角度路由器将带有子级的路由视为非终端路由,并且路由仅发生在终端路由上. Angular路由器希望路由具有path: ''
的默认条目.
Angular router considers a route with children as a non-terminal route and routing happens to terminal routes only. Angular router expects route to have a default entry for path: ''
.
要解决此问题,您应该添加从父路由到子路由之一的重定向.
To resolve this issue you should add a redirect from the parent route to one of the child routes.
export const AdminRoutes: RouterConfig = [
{
path: 'admin',
component: AdminComponent,
children: [
{ path: '', redirectTo: 'main-page', terminal: 'true' },
{ path: 'main-page', component: MainPageComponent,
{ path: 'settings', component: SettingsComponent },
]
}];
如果使用rc4和路由器3.0.0-beta2,则它们已将terminal
重命名为pathMatch
.因此,如下更新重定向路由:
if using rc4 and router 3.0.0-beta2 they have renamed terminal
to pathMatch
. So update the redirect route as below:
{ path: '', redirectTo: 'main-page', pathMatch: 'full'},
这篇关于如何在Angular中实现多级路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!