如何在父组件中解析数据
How to get data resolved in parent component
我使用Vue v1.0.28
和vue-resource
调用我的API并获得资源数据。所以我有一个父组件,叫做Role,它有一个子组件InputOptions。它有一个遍历角色的foreach
。
所有这一切的大局是一个可以选择的项目列表,所以API可以返回预先选择的项目,因为用户以前保存/选择了它们。关键是我不能填充InputOptions的selectedOptions
。我怎样才能从父组件获得这些信息呢?这是正确的做法,对吗?
我在这里粘贴了一段代码,试图更好地显示我的问题:
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
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React-将jSON数据传递给子组件的子组件
- 如何在ReactJs中渲染子组件时重新加载子组件的数据
- 在reactjs组件中预加载数据
- 具有大型数据集的组件仅在 IE11/Edge 上运行缓慢
- 使用数据数组创建多个类似组件
- 如何正确处理依赖ViewChild和Angular 2中可观察到的数据的组件上的更改检测
- Angular2路由-将数据从父组件传递到子组件
- 编辑组件中的Vuejs数据
- 在多个react.js组件中呈现json数据
- 通过使用此组件,检索父类数据
- 如何创建一个组件,我可以设置在 Angular 2 中的属性中获取 URL 数据
- 如何使用Vue.js(使用Vueify)通过组件显示数据
- Vue.js从另一个组件获取数据
- Sencha Touch:如何在setData()之后刷新组件的数据
- 如何更新angular2中组件之间的事件数据
- 如何访问Vue.js中深度嵌套的子组件中的数据
- 将数据传回父组件的ReactJS组件无法工作
- 如何处理需要子组件状态的Meteor数据
- 根据location.hash传递的数据重新呈现ReactJS嵌套的子组件