如何在 Angular2 中导航到精确的 url 字符串

How to navigate to an exact url string in Angular2

我有一个设置了参数的路由器

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

在其他地方,我正在尝试读取看起来像这样的链接的 href 值

href="article/45/titlehere"

并使用路由器导航方法路由到那个 url,而不必将其分解为单个参数。 Angular2 中有直接的方法来做到这一点吗?像这样?

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

这不起作用,继续获取

EXCEPTION: TypeError: linkParams.reduce is not a function

PS。已正确设置为站点根

如果你有 url 作为字符串,你应该使用 Router 对象的 navigateByUrl 方法:

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.