我想将对象中的所有属性作为 传递props,并且v-bind不带参数使用。
但是如何props在不必在子组件中声明道具的情况下获得子组件?
例如,在下面的代码中item是一个对象。
父组件:
<div v-for="item in domArr" :key="item.id">
<cus-dom v-bind="item"></cus-dom>
</div>
Run Code Online (Sandbox Code Playgroud)
子组件:
<script>
export default {
name: 'cusDom',
props: [], // HOW TO GET THE props, because I have it empty/no arguments HERE?
data() {
return {};
},
mounted() {
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
即使在使用时v-bind,您仍然必须将它们声明为props.
如果你不这样做,他们就会$attrs。
看下面的demo,应该就清楚了。
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}
}
})Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3005 次 |
| 最近记录: |