gpt4 book ai didi

vue.js - 有条件地渲染父元素,保留内部 html

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:23 24 4
gpt4 key购买 nike

是否有任何内置方法可以有条件地显示父元素?

举例说明:

<a v-show-but-keep-inner="someCondition">
<span>This is always rendered no matter what</span>
</a

最佳答案

我认为这是自定义指令的工作。我将其作为快速 POC 制作:

Vue.directive('showButKeepInner', {
bind(el, bindings) {
bindings.def.wrap = function(el) {
// Find all next siblings with data-moved and move back into el
while (el.nextElementSibling && el.nextElementSibling.dataset.moved) {
el.appendChild(el.nextElementSibling).removeAttribute('data-moved')
}
el.hidden = false
}

bindings.def.unwrap = function(el) {
// Move all children of el outside and mark them with data-moved attr
Array.from(el.children).forEach(child => {
el.insertAdjacentElement('afterend', child).setAttribute('data-moved', true)
})
el.hidden = true
}
},

inserted(el, bindings) {
bindings.def[bindings.value ? 'wrap' : 'unwrap'](el)
},

update(el, bindings) {
bindings.def[bindings.value ? 'wrap' : 'unwrap'](el)
}
})

new Vue({
el: '#app',
data: {
someCondition: false
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.min.js"></script>

<div id="app">
<p>
<button v-on:click="someCondition = !someCondition">{{ someCondition }}</button>
</p>
<a v-show-but-keep-inner="someCondition" href="/">
<span>This is always rendered no matter what</span>
</a>
</div>

关于vue.js - 有条件地渲染父元素,保留内部 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43293401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com