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

时间:2017-03-13 15:56:24

标签: javascript angular angular2-routing

我希望基本路径http://localhost:3000//在我的应用中重定向到/wiki

enter image description here

enter image description here

我的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(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 { }

2 个答案:

答案 0 :(得分:0)

正如@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 { }

答案 1 :(得分:0)

我能够修复应用程序的Node部分中的重定向问题。仍然会等待看看是否有更好的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);
});