使用angular2 rc4将所有无效网址重定向到特定组件

时间:2016-07-02 04:57:03

标签: typescript angular angular-routing

我正在使用"@angular/router": "3.0.0-beta.2"编写angular2 rc4应用程序。

现在我有两条路线,欢迎和帮助,我创建了另一条路线,将所有其他路由重定向到欢迎组件。

这是我的routes.ts文件:

import { provideRouter, RouterConfig } from "@angular/router";
import {WelcomeComponent} from "./welcome.component";
import {HelpComponent} from "./help.component";

export const routes:RouterConfig = [
    { path: "",redirectTo: "welcome"},
    { path: "welcome", component: WelcomeComponent },
    { path: "help",component: HelpComponent}
];

export const APP_ROUTER_PROVIDERS = [
    provideRouter(routes)
];

我的服务器是GoLang,我已将其配置为所有无效的网址都会重定向到index.html

所以现在如果我浏览例如:localhost/weclome2, 它确实显示了内容欢迎组件,但浏览器上的URL仍指向welcome2而不是welcome,我在javascript控制台中也出现以下错误:

EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: 'welcome2'
VM8530:27 EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: 'welcome2'window.console.error @ VM8530:27BrowserDomAdapter.logError @ bundle.js:50349BrowserDomAdapter.logGroup @ bundle.js:50359ExceptionHandler.call @ bundle.js:11343(anonymous function) @ bundle.js:14458schedulerFn @ bundle.js:14747SafeSubscriber.__tryOrUnsub @ bundle.js:15377SafeSubscriber.next @ bundle.js:15326Subscriber._next @ bundle.js:15279Subscriber.next @ bundle.js:15243Subject.next @ bundle.js:14835EventEmitter.emit @ bundle.js:14735onError @ bundle.js:19058onHandleError @ bundle.js:19270ZoneDelegate.handleError @ bundle.js:5267Zone.runGuarded @ bundle.js:5173_loop_1 @ bundle.js:5427drainMicroTaskQueue @ bundle.js:5434ZoneTask.invoke @ bundle.js:5366
VM8530:27 STACKTRACE:window.console.error @ VM8530:27BrowserDomAdapter.logError @ bundle.js:50349ExceptionHandler.call @ bundle.js:11345(anonymous function) @ bundle.js:14458schedulerFn @ bundle.js:14747SafeSubscriber.__tryOrUnsub @ bundle.js:15377SafeSubscriber.next @ bundle.js:15326Subscriber._next @ bundle.js:15279Subscriber.next @ bundle.js:15243Subject.next @ bundle.js:14835EventEmitter.emit @ bundle.js:14735onError @ bundle.js:19058onHandleError @ bundle.js:19270ZoneDelegate.handleError @ bundle.js:5267Zone.runGuarded @ bundle.js:5173_loop_1 @ bundle.js:5427drainMicroTaskQueue @ bundle.js:5434ZoneTask.invoke @ bundle.js:5366
VM8530:27 Error: Uncaught (in promise): Error: Cannot match any routes: 'welcome2'
at resolvePromise (bundle.js:5478)
at bundle.js:5455
at ZoneDelegate.invoke (bundle.js:5263)
at Object.onInvoke (bundle.js:19249)
at ZoneDelegate.invoke (bundle.js:5262)
at Zone.run (bundle.js:5156)
at bundle.js:5511
at ZoneDelegate.invokeTask (bundle.js:5296)
at Object.onInvokeTask (bundle.js:19240)
at ZoneDelegate.invokeTask (bundle.js:5295)window.console.error @ VM8530:27BrowserDomAdapter.logError @ bundle.js:50349ExceptionHandler.call @ bundle.js:11346(anonymous function) @ bundle.js:14458schedulerFn @ bundle.js:14747SafeSubscriber.__tryOrUnsub @ bundle.js:15377SafeSubscriber.next @ bundle.js:15326Subscriber._next @ bundle.js:15279Subscriber.next @ bundle.js:15243Subject.next @ bundle.js:14835EventEmitter.emit @ bundle.js:14735onError @ bundle.js:19058onHandleError @ bundle.js:19270ZoneDelegate.handleError @ bundle.js:5267Zone.runGuarded @ bundle.js:5173_loop_1 @ bundle.js:5427drainMicroTaskQueue @ bundle.js:5434ZoneTask.invoke @ bundle.js:5366
VM8530:27 Unhandled Promise rejection: Cannot match any routes: 'welcome2' ; Zone: angular ; Task: Promise.then ; Value: Error: Cannot match any routes: 'welcome2'(…)window.console.error @ VM8530:27consoleError @ bundle.js:5401_loop_1 @ bundle.js:5430drainMicroTaskQueue @ bundle.js:5434ZoneTask.invoke @ bundle.js:5366
VM8530:27 Error: Uncaught (in promise): Error: Cannot match any routes: 'welcome2'(…)window.console.error @ VM8530:27consoleError @ bundle.js:5403_loop_1 @ bundle.js:5430drainMicroTaskQueue @ bundle.js:5434ZoneTask.invoke @ bundle.js:5366

我错过了什么?

谢谢!

更新

根据谷歌搜索,我在旧版本的angular2上找到了一些回复,所以我尝试使用以下代码将它们用于新版本:

export const routes:RouterConfig = [
    { path: "",redirectTo:"welcome",pathMatch:"full"},
    { path: "welcome", component: WelcomeComponent },
    { path: "help",component: HelpComponent},
    { path: "*",redirectTo:"welcome"}
];

但结果完全一样。

1 个答案:

答案 0 :(得分:11)

新路由器使用'**'而不是单个'*'

podfile