如何在父组件中解析数据

How to get data resolved in parent component

本文关键字:数据 组件      更新时间:2023-09-26

我使用Vue v1.0.28vue-resource调用我的API并获得资源数据。所以我有一个父组件,叫做Role,它有一个子组件InputOptions。它有一个遍历角色的foreach

所有这一切的大局是一个可以选择的项目列表,所以API可以返回预先选择的项目,因为用户以前保存/选择了它们。关键是我不能填充InputOptionsselectedOptions。我怎样才能从父组件获得这些信息呢?这是正确的做法,对吗?

我在这里粘贴了一段代码,试图更好地显示我的问题:

role.vue

<template>
    <div class="option-blocks">
    <input-options
        :options="roles"
        :selected-options="selected"
        :label-key-name.once="'name'"
        :on-update="onUpdate"
        v-ref:input-options
    ></input-options>
    </div>
</template>
<script type="text/babel">
    import InputOptions from 'components/input-options/default'
    import Titles from 'steps/titles'
    export default {
        title: Titles.role,
        components: { InputOptions },
        methods: {
            onUpdate(newSelectedOptions, oldSelectedOptions) {
                this.selected = newSelectedOptions
            }
        },
        data() {
            return {
                roles: [],
                selected: [],
            }
        },
        ready() {
            this.$http.get('/ajax/roles').then((response) => {
                this.roles = response.body
                this.selected = this.roles.filter(role => role.checked)
            })
        }
    }
</script>

InputOptions

<template>
    <ul class="option-blocks centered">
        <li class="option-block" :class="{ active: isSelected(option) }" v-for="option in options" @click="toggleSelect(option)">
            <label>{{ option[labelKeyName] }}</label>
        </li>
    </ul>
</template>
<script type="text/babel">
    import Props from 'components/input-options/mixins/props'
    export default {
        mixins: [ Props ],
        computed: {
            isSingleSelection() {
              return 1 === this.max
            }
        },
        methods: {
            toggleSelect(option) {
                //...
            },
            isSelected(option) {
                return this.selectedOptions.includes(option)
            }
        },
        data() {
            return {}
        },
        ready() {
            // I can't figure out how to do it
            // I guess it's here where I need to get that information,
            // resolved in a promise of the parent component
            this.$watch('selectedOptions', this.onUpdate)
        }
    }
</script>

道具

export default {
    props: {
        options: {
            required: true
        },
        labelKeyName: {
            required: true
        },
        max: {},
        min: {},
        onUpdate: {
            required: true
        },
        noneOptionLabel: {},
        selectedOptions: {
            type: Array
            default: () => []
        }
    }
}

编辑

我现在在控制台中得到这个警告:

[Vue warn]: Data field "selectedOptions" is already defined as a prop. To provide default value for a prop, use the "default" prop option; if you want to pass prop values to an instantiation call, use the "propsData" option. (found in component: <default-input-options>)

您使用Vue.js 2.0.3版本吗?如果存在,则不存在http://vuejs.org/api中指定的ready函数。您可以在组件的created钩子中这样做:

// InputOptions component
// ...
data: function() {
    return {
        selectedOptions: []
    }
},
created: function() {
    this.$watch('selectedOptions', this.onUpdate)
}
在您的InputOptions组件中,您有以下代码:
this.$watch('selectedOptions', this.onUpdate)

但我无法看到methods中定义的onUpdate函数。相反,它在父组件role中定义。你能插入一个console.log("selectedOptions updated")来检查它是否按照你的期望被调用吗?我认为Vue.js希望方法出现在同一个组件中。

或者在上述情况下,我认为您可以在this.$watch(...)内执行this.$parent.onUpdate -我没有尝试过,但可能适合您。

编辑:一些想法

你可能有更多的问题-你试图观察一个数组- selectedOptions,这是一个冒险的策略。数组不会改变——它们就像对象列表的容器。但是内部的单个对象会改变。因此,$watch可能不会触发selectedOptions

根据我到目前为止使用Vue.js的经验,我观察到当你添加或删除一个项目时,数组的变化是注册的,而不是当你改变一个对象时-这需要你自己验证。

为了解决这个问题,您可以为每个输入选项设置单独的组件(input-one-option),这样更容易观察变化。

最后,我找到了这个bug。我没有将道具绑定为kebab-case