2016-07-02 18 views
3

我正在用"@angular/router": "3.0.0-beta.2"編寫angular2 rc4應用程序。使用angular2 rc4將所有無效url重定向到特定組件

現在我有兩條路線,歡迎和幫助,並且我創建了另一條路線,將其他路線重新指向歡迎組件。

這是我的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而且我已經配置了所有無效的URL重定向到index.html的

所以現在如果我瀏覽例如:它確實顯示我的內容歡迎組件,但瀏覽器上的網址仍然指向welcome2而不是歡迎,我也在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"} 
]; 

但結果一模一樣。

回答

9

新的路由器使用 '**',而不是一個單一的 '*'

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