React-Native 导航(v5) DeepLinking 从 URL 获取参数

发布于 2025-01-10 05:03:15 字数 1048 浏览 5 评论 0原文

知道如何获得正确的参数吗?

我收到此URL并且我想导航到正确参数内的正确屏幕。

网址-> /p-einhell-drill-screwdriver-123456/

我想采用 url 3 个参数的形式:

  1. URL 是 -> p-einhell-drill-screwdriver-123456/
  2. seoName ->; einhell 钻头螺丝刀
  3. id -> 123456

我可以将所有这些都放在一个参数中,但我不能将它们分散到 3 个参数中。 目前我有这个解析器,但它只需要一个参数。

例如:

const config = {
     ProductScreen: {
                       path: ':seoName/',   /* p-einhell-drill-screwdriver-123456/ */
                       parse: {
                          seoName: (seoName) =>
                             seoName.split('-').slice(1, -1).join('-'),
                          URL: (seoName) => seoName,
                          currentProductId: (seoName) =>
                             seoName.split('-').pop(),
                       },
                    },
}

结果:

<前><代码>参数:{ 网址:未定义 当前产品 ID:未定义 seoName: "einhell-drill-screwdriver" }

Have any ideas how can I get correct params ?

I getting this URL and I want navigate to correct screen within correct params.

URL -> /p-einhell-drill-screwdriver-123456/

I want to take form that url 3 parameters:

  1. URL which is -> p-einhell-drill-screwdriver-123456/
  2. seoName -> einhell-drill-screwdriver
  3. id -> 123456

I can take all of them in one parameter but i can not spread in 3 param.
At this moment I have this parser but It takes only one params.

Forxample:

const config = {
     ProductScreen: {
                       path: ':seoName/',   /* p-einhell-drill-screwdriver-123456/ */
                       parse: {
                          seoName: (seoName) =>
                             seoName.split('-').slice(1, -1).join('-'),
                          URL: (seoName) => seoName,
                          currentProductId: (seoName) =>
                             seoName.split('-').pop(),
                       },
                    },
}

Result:

  params: {

  URL: undefined

  currentProductId: undefined

  seoName: "einhell-drill-screwdriver"    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2025-01-17 05:03:15

parse 下的选项引用配置中定义的参数(例如 :seoName)和查询参数(如果有)。您不能在那里添加任意属性。

但是您可以在 seoName 中返回一个对象:

parse: {
  seoName: URL => {
    const seoName = URL.split('-').slice(1, -1).join('-');
    const currentProductId = URL.split('-').pop();

    return { URL, seoName, currentProductId };
  }
}

然后在组件中的 route.params.seoName 中获取该对象。

The options under parse refer to the params defined in the config (e.g. :seoName) and query params if any. You can't add arbitrary properties there.

But you can return an object in seoName:

parse: {
  seoName: URL => {
    const seoName = URL.split('-').slice(1, -1).join('-');
    const currentProductId = URL.split('-').pop();

    return { URL, seoName, currentProductId };
  }
}

And then get this object in route.params.seoName in your component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文