angular - 如何导航到 Angular2 中的确切 url 字符串

标签 angular angular2-routing

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

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

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

href="article/45/titlehere"

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

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

这个不行,继续获取

EXCEPTION: TypeError: linkParams.reduce is not a function

附言。已正确设置为站点根目录

最佳答案

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

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.

关于angular - 如何导航到 Angular2 中的确切 url 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805244/

相关文章:

javascript - Highcharts 气泡图数据标签中的居中文本

Angular w/Jest : `verify()` vs .`expectOne()` ?

javascript - Angular 6,请求httpClient(类型错误)

javascript - 如何检测 Angular 2 中的 URL 变化?

node.js - "How to build a chatbot with anuglar and node.js"?

angular - 由 .Net Core 托管的 Angular 应用程序中的 robots.txt 文件

angular - TypeScript 要求将类型添加到键值

html - 如何防止data-target和routerLink打开新窗口?

javascript - 无法绑定(bind)到 'routerlink',因为它不是使用 angular2-webpack-starter 的已知 native 属性

angular - 当 ng2-nvd3 图表不可见时,浏览器会出现大小变化错误