Dee*_*eep 0 vue.js vue-component vuejs2
我有组件:
Vue.component('child', {
template : '#child-tpl',
props : {
child : Object
}
},
methods : {
index : function () {
return ???; // current index
}
}
});
Run Code Online (Sandbox Code Playgroud)
这些孩子可以重新排序/删除/添加。需要存储该子项的实际当前索引。如何获取父子数组的目标子对象的当前索引?
传递索引作为道具。该索引来自孩子之外的某个地方,因此孩子应该将其作为道具。孩子中不应有任何向父母查询信息的方法。孩子从外部本身需要的所有东西都应作为道具传递给它。
在下面的代码段中,索引由方便地提供v-for。
Vue.component('child', {
template: '#child-tpl',
props: ['child', 'index']
});
new Vue({
el: '#app',
data: {
children: ['a', 'b', 'c', 'd']
},
methods: {
reverse: function () {
this.children.reverse();
}
}
});Run Code Online (Sandbox Code Playgroud)
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.2/vue.min.js"></script>
<template id="child-tpl">
<div>I'm {{child}}, {{index}}</div>
</template>
<div id="app">
<child v-for="(child, index) in children" :child="child" :index="index"></child>
<button @click="reverse">Reverse</button>
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1867 次 |
| 最近记录: |