Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect all invalid urls to a specific component with angular2 rc4

I'm writing an angular2 rc4 application with "@angular/router": "3.0.0-beta.2".

for now I have two routes, welcome and help, and I created another route that will rediret everything else to the welcome component.

this is my routes.ts file:

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

my server is GoLang and I have it configured that all invalid urls will redirect to index.html

so now if I browse for example: localhost/weclome2, it does shows me the content welcome component, but the url on the browser still points to welcome2 instead of welcome and I also get the following error in javascript console:

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

what am I missing ?

thanks!

update

According to a google search I found some responses on older versions of angular2, so I tried to adopt them to the new version with the following code:

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

but the results are exactly the same.

like image 640
ufk Avatar asked Dec 06 '22 17:12

ufk


1 Answers

The new router uses '**' as opposed to a single '*'

export const routes:RouterConfig = [
    { path: "",redirectTo:"welcome",pathMatch:"full"},
    { path: "welcome", component: WelcomeComponent },
    { path: "help",component: HelpComponent},
    { path: "**",redirectTo:"welcome"}
];
like image 116
Ian Belcher Avatar answered Dec 09 '22 14:12

Ian Belcher