【问题标题】:How to access parents's props value in vue component object?如何在 vue 组件对象中访问父母的道具值?
【发布时间】:2017-04-25 08:31:29
【问题描述】:

我正在尝试在元素 UI 工具包中创建两个具有多个限制的选项组。在我的例子中,我调用了一个@handleOptionClick 的方法来防止根据multiple-limit 值进行选择。我如何在handleGroupedOption 函数中访问这个multiple-limit

<el-select v-model="filterBy[filterKey]" @handleOptionClick="handleGroupedOption" placeholder="Filter By Incident" multiple>
    <el-option-group label="Select one" multiple-limit="1">
        <el-option
                v-for="(item, key, index) in makeList(mainTypes[filterKey])"
                v-if="index < 3"
                :label="item"
                :value="key">
        </el-option>
    </el-option-group>
    <el-option-group label="Select multiple" multiple-limit="0">
        <el-option
                v-for="(item, key, index) in makeList(mainTypes[filterKey])"
                v-if="index >= 3"
                :label="item"
                :value="key">
        </el-option>
    </el-option-group>
</el-select>


export default {
  methods: {
    handleGroupedOption (event) {
     console.log(event)
    }
  }
}

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    您应该将父母的属性传递给孩子。

    家长:

        <template>
           <div class="parent">
               <my-child :mydata="mydata"></my-child>
           </div>
        </template>
    
        export default {
          data: function() {
            return { mydata: ["a", "b"] }
          }
        }
    

    孩子

        <template>
           <div class="child">
               <div v-for="data in mydata">
                  {{ data }}
               </div>
           </div>
        </template>
    
        export default {
          props: ['mydata']
        }
    

    另一种选择当然是通过 this.$parent 直接在您的子组件中访问父级

    你可以为此定义一个计算属性

        computed {
          parentData: function() {
             return this.$parent.$data; // or whatever you want to access
          }
        }
    

    【讨论】:

    • 我没找到你,你能给我提一下我的案子(两组选择)吗?
    • 好吧,你能给我一个小提琴吗?如果您希望小提琴中有一个样本,您至少应该自己提供一个小提琴。
    猜你喜欢
    • 2021-07-31
    • 2020-10-12
    • 1970-01-01
    • 2019-05-24
    • 2017-06-05
    • 2019-11-11
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    相关资源
    最近更新 更多