我希望以props的形式传递对象中的所有属性,并使用不带参数的v-bind。
但是,我如何才能在子组件中获得props而不必在子组件中声明道具呢?
例如,在下面的代码中,item 是一个对象.
父组件:
<div v-for="item in domArr" :key="item.id">
<cus-dom v-bind="item"></cus-dom>
</div>子组件:
<script>
export default {
name: 'cusDom',
props: [], // HOW TO GET THE props, because I have it empty/no arguments HERE?
data() {
return {};
},
mounted() {
}
}
</script>发布于 2018-03-18 06:44:58
即使在使用v-bind时,仍然必须将它们声明为props。
如果你不这样做,他们将是$attrs。
请参阅下面的演示程序,应该清楚说明。
Vue.component('child', {
props: ['a'],
template: `<button @click="go">PROPS AND ATTRS</button>`,
mounted() {
this.go()
},
methods: {
go() {
console.log(
'$attrs:', JSON.stringify(this.$attrs), '- $props:', JSON.stringify(this.$props),
'- a:', this.a, '- b:', this.b)
}
}
});
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!',
stuff: {a: 1, b: 2}
}
})<script src="https://unpkg.com/vue@2.5.16/dist/vue.min.js"></script>
<div id="app">
<p>{{ message }}</p>
<child v-bind="stuff"></child>
</div>
https://stackoverflow.com/questions/49345098
复制相似问题