【问题标题】:get id value of url in another component passed via props - vue获取通过道具传递的另一个组件中url的id值 - vue
【发布时间】:2021-05-05 11:33:10
【问题描述】:

我正在创建一个可重用的组件。有两个子组件。这个流程如下所示

Child component 1 -> Parent Component -> Super Parent main Component

在子组件中,我正在创建一个 prop url,然后传递给父组件,然后传递给超级父组件,我在其中传递如下所示的值。

子组件

prop: {
    urls: {
            type: Object,
            required: true,
            default: () => ({
               saveProduct: '/entity/save',
               updateProduct: '/entity/update
            })
        }
}

父组件

    <ChildComponent :urls = "uris" />

    props: {
      uris: {
          type:Object,
          required:true,
          default: () => ({})
      }
   }

超级父组件

<ParentComponent :uris="urls" />

 data() {
  return {
    urls: {
          saveUri: `/products/${this.$route.params.id}/categories`,
          updateUri: `/products/${this.$route.params.id}/categories/${this.productId}`
      }
  }
 }

在这里,我给出的是用于流的 sn-ps,而不是完整的代码。我想知道在 Super parent 中获取 productId 值的通用方法。

在这里,我可以得到这个 (this.$route.params.id) 值,但是我怎样才能从子组件中获取超级父级中可用的 productid 值。

任何通用示例都会对我有所帮助...如何将数据从一个组件发送到下一个组件的下一个组件?

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    Props 用于将数据从父级向下传递给子级。要将数据从子级向上发送到父级,emit 和子级的事件并在父级中收听。

    this.$emit('clicked', 'someValue')
    

    另一种选择是使用像 Vuex 这样的全局状态存储。

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2018-11-20
      • 1970-01-01
      • 2017-10-10
      • 2018-07-15
      • 2020-12-21
      • 2020-04-03
      • 1970-01-01
      • 2019-11-08
      相关资源
      最近更新 更多