【问题标题】:How to always redirect from base / to specific path in Angular2如何始终从基/重定向到Angular2中的特定路径
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • /wiki 路径应该排在最后(顺序很重要,更具体的路径应该排在最前面)
  • @GünterZöchbauer 如果我们考虑顺序,空路径(路径:'')不应该最后出现吗?
  • 没有冲突,尤其是pathMatch: 'full',但是以wiki/... 开头的3 个路径冲突和wiki/category/...wiki/entity/... 将永远无法到达。您还应该删除path: '/wiki' 之前的前导/。路径不允许有前导 / (虽然 redirectTo 很好)
  • 谢谢,我试过了,但仍然是同样的问题:Listening on: http://localhost:3000 GET / 404 5.761 ms - 46 它命中了/,但没有转到/wiki 我也尝试了推荐的编辑。
  • 顺便说一句,我不得不在新的/wiki 路径前面去掉/
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2012-11-25
    • 2015-02-09
    • 2016-12-03
    • 2013-03-03
    相关资源
    最近更新 更多