【发布时间】:2017-03-13 15:56:23
【问题描述】:
我希望基本路径 http://localhost:3000/ 或 / 在我的应用程序中重定向到 /wiki。
我的 app.routing.ts 文件
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CategoryComponent } from './category/category.component';
import { EntityComponent } from './entity/entity.component';
@NgModule({
imports: [
RouterModule.forChild([
{ path: '', redirectTo: '/wiki', pathMatch: 'full' },
{ path: 'wiki/category/:id', component: CategoryComponent },
{ path: 'wiki/entity/:id', component: EntityComponent }
])
],
})
export class AppRoutingModule { }
我以为这就够了:{ path: '', redirectTo: '/wiki', pathMatch: 'full' }
也试过{ path: '/', redirectTo: '/wiki', pathMatch: 'full' }
有人知道我错过了什么吗?
我还尝试将HomeComponent 添加到/wiki 的路径中。转到 localhost:3000 仍然没有重定向到 /wiki,当我手动输入并转到 /wiki 时出现以下错误:
错误:路由''的配置无效:redirectTo 和组件不能一起使用 在 validateNode (/Users/leongaban/Projects/TickerTags/wikitags/dist/server/index.js:54088:15) 在 Array.forEach(本机)
代码
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CategoryComponent } from './category/category.component';
import { EntityComponent } from './entity/entity.component';
import { HomeComponent } from './home/home.component';
@NgModule({
imports: [
RouterModule.forChild([
{ path: '', redirectTo: '/wiki', pathMatch: 'full', component: HomeComponent },
{ path: 'wiki/category/:id', component: CategoryComponent },
{ path: 'wiki/entity/:id', component: EntityComponent }
])
]
})
export class AppRoutingModule { }
【问题讨论】:
-
够了,但
/wiki不是您的配置中的有效路径。你需要/wiki/category/1或类似的 -
@GünterZöchbauer?你能解释一下sn-p吗?我已经有一个
wiki/category/:id路径。如果应用程序的基本路径被命中,我试图强制重定向到/wiki。 -
@GünterZöchbauer 的意思是您的路线中需要
{ path: '/wiki', component: WikiComponent } -
@YounesM 哦,我也试过了,但没用。它给了我这个错误:
Unhandled Promise rejection: Invalid configuration of route '': redirectTo and component cannot be used together -
请展示“也尝试过”的样子。
标签: javascript angular angular2-routing