【问题标题】:Angular: Path when url parameter has ? in itAngular:url参数有时的路径?在里面
【发布时间】:2020-08-31 02:24:45
【问题描述】:

我有类似 localhost:4200/user/contact 和 localhost:4200/user/contact/1 的 URL,其中“1”是返回 id 的参数。

为此,我的 routing.ts 就像...

const appRoutes: Routes = [
    { path: 'user',
      children: [
        {
          path="",
          component: UserComponent,
        },
        {
          path="contact",
          component: UserContactComponent,
        },
        {
          path="contact/:id",
          component: UserComponent,
        },
      ]
    },
];`

但是当 URL 是 localhost:4200/user/contact/1?name=abc&value=65767 时它就不起作用 "1?name=abc&value=65767" 不作为单个参数返回。在这种情况下,我的路径应该是什么?

【问题讨论】:

    标签: angular url parameters path routes


    【解决方案1】:

    所有跟在 ? 后面的 key=value字符被视为 queryParams 而不是“路径”参数。

    要获取这些值,您必须在用户组件中的 ActivatedRouteSnapshot 上使用 queryParamMap

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多