Ayyash
Ayyash

Reputation: 4409

How to navigate to an exact url string in Angular2

I have a router set up with parameters

@RouteConfig([
    {
        path: 'article/:id/:title',
        name: 'ArticleContent',
        component: ArticleComponent
    }
])

Somewhere else, I am trying to read the href value of links that looks like this

href="article/45/titlehere"

and use Router navigate method to route to that url without having to break it up to single params. Is there a straight way in Angular2 to do that? like this?

_router.navigate("article/34/titlehere")

This isn't working, keep getting

EXCEPTION: TypeError: linkParams.reduce is not a function

PS. is already set correctly to site root

Upvotes: 4

Views: 2852

Answers (1)

dfsq
dfsq

Reputation: 193291

If you have url as a string you should use navigateByUrl method of Router object:

navigateByUrl(url: string, _skipLocationChange?: boolean) : Promise<any>

Navigate to a URL. Returns a promise that resolves when navigation is complete. It's preferred to navigate with navigate instead of this method, since URLs are more brittle.

If the given URL begins with a /, router will navigate absolutely. If the given URL does not begin with /, the router will navigate relative to this component.

Upvotes: 3

Related Questions