如何始终从 base / 重定向到 Angular2 中的特定路径

How to always redirect from base / to specific path in Angular2

我希望基本路径 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 时出现以下错误:

Error: Invalid configuration of route '': redirectTo and component cannot be used together at validateNode (/Users/leongaban/Projects/TickerTags/wikitags/dist/server/index.js:54088:15) at Array.forEach (native)

代码

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 { }

正如@GünterZöchbauer 所说,您需要定义要重定向到的路径。这是一个片段。

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 },
            { path: '/wiki', component: HomeComponent }
        ])
    ],
})
export class AppRoutingModule { }

我能够解决应用程序节点部分的重定向问题。仍然会等着看是否有更好的 Angular 客户端解决这个问题。

我刚刚将路径添加到 / 并将其重定向到 /wiki:

app.get('/', function(req, res) {
    return res.redirect('/wiki');
});

app.get('*', function(req, res) {
    res.setHeader('Content-Type', 'application/json');
    var pojo = { status: 404, message: 'No Content' };
    var json = JSON.stringify(pojo, null, 2);
    res.status(404).send(json);
});