Angular 2使用参数将当前URL路由 [英] Angular 2 routes current url with params

查看:164
本文介绍了Angular 2使用参数将当前URL路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Angular 2 rc-4与新路由器一起使用.

I'm trying to use Angular 2 rc-4 with new Router.

export const appRoutes:  
  RouterConfig =[
  {
    path: "",
    component: HomeComponent,
    terminal: true,
    data: {
            title: "Home"
          }
   },
   {
     path: "Users",
     component: UserListComponent,
     data: {
            title: "Users"
           }
      },
      {
        path: "Users/:id",
        component: UserEditComponent,
        data: {
              title: "Edit User"
               }
      }
 ];

然后,我订阅更改路线事件. 事件触发时,我想从中查找项目

Then I subscribe to change route event. When event fires I want to find item from

appRoutes

问题在于带有参数的网址:

The problem is with url with params:

Users/:id

我不知道如何将其与当前页面网址进行比较,或者如何询问RouterConfig.

I don't know how to compare it with current page url or how to ask RouterConfig.

你能帮我吗?

推荐答案

我发现,最好的解决方案是使用订阅来更改路线事件来获取当前路线.根据此路线,您可以获取所有数据并检查参数. 如果您有更好的解决方案,请也添加您的答案.

AS I found, best solution is to get current route using subscription to route change event. Based on this route you can get all data and check params. If you have better solution please add your answer too.

这篇关于Angular 2使用参数将当前URL路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆