繁体   English   中英

如何始终从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时,出现以下错误:

错误:路线''的无效配置:redirectTo和组件不能在Array.forEach(本地)的validateNode(/Users/leongaban/Projects/TickerTags/wikitags/dist/server/index.js:54088:15)上一起使用

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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM