[英]Toggle button in dynamic/recursive structure
I'm looping an array of element
and I'd want to recursively display that element
with given template
我正在循环一个element
数组,我想用给定的template
递归地显示该element
and then inside that template use button with toggle to show/hide deeper level template of Child
s of given element
( Child
is also an Element
) 然后在该模板内部使用带有切换按钮的按钮来显示/隐藏给定element
的Child
的更深层次的模板( Child
也是Element
)
<div v-for="(element in elements)">
<MyTemplate :element="element"></MyTemplate>
</div>
Here's my template: 这是我的模板:
<template>
<div>element.Name</div>
<button @click="toggleSomehow">
// I'd want to display it under that button when he's "showing"
<MyTemplate :element="element.Child"></MyTemplate>
</button>
</template>
But I'm not really sure how to do that SHOW
/ HIDE
button without binding it with some property or array, but I'd rather want to avoid it because everything has to be kind of dynamic 但是我不确定如何在不将其与某些属性或数组绑定的情况下执行SHOW
/ HIDE
按钮,但是我宁愿避免使用它,因为一切都必须是动态的
You should add toggleable data to your MyComponent
component like visible
See example below 您应该将可切换数据添加到MyComponent
组件中,如visible
请参见下面的示例
Vue.component('my-template', { template: '#my-template', props: { element: { type: Object, required: true } }, data() { return { visible: false } }, methods: { toggleVisible() { this.visible = !this.visible } } }); new Vue({ el: '#app' })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <script type="text/x-template" id="my-template"> <div> <div>{{element.name}}</div> <button @click="toggleVisible" v-if="element.child">toggle</button> <my-template v-if="visible" :element="element.child" /> </div> </script> <div id="app"> <my-template :element="{name: 'test', child: {name: 'child test'}}" /> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.