4

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding link to documentation for reference (It is not quite easy to reach these files on Angular website) angular.io/docs/ts/latest/api/router/Router-class.html
@Ayyash, link is broken.
yup, angular moves quite fast, now its somebody else's chore to find the right link :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.