結論
cssにてdisplay:noneで非表示にした項目に対し
v-showを用いて表示の切り替えができるか。
v-showは上からdisplay:noneを付けるだけで
cssで書かれたdisplay:noneを無効にはできないのでしょうか。
出来ないようです。Vue.js開発者のEvan Youが本質問と同様の質問に以下のように回答をしています。
No, because depending on different elements, there could be different default display properly, block is not the only possible value, so we simply set it to an empty value when v-show value is true. Also, v-show has to take into account inline display properties like style="display: inline" v-show="xxx".
https://github.com/vuejs/vue/issues/4238#issuecomment-261580863
その上で、以下、回答です。
cssをdisplay:blockなどで上書きすれば動くが、よりスマートな方法があるか知りたいです。
よりスマートかどうかはわからないですが、custom directive
を作って対応するか、display:block
で上書きするクラスを追加するかとかですかね。
以下のコードではcustom directive
をローカルインストールして使っていますけど、グローバルインストールでもOKです。
デモコード
<template>
<div id="app">
<p class="original" :class="{myStyle: isVisible}" v-show="isVisible">Hello</p>
<p class="original" v-customShow="isVisible">World</p>
<button @click="isVisible = !isVisible">Toggle</button>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
isVisible: true
}
},
directives: {
customShow (el, binding) {
if (binding.value) {
el.style.display = "block"
} else {
el.style.display = "none"
}
}
}
};
</script>
<style>
.original {
display: none;
}
.myStyle {
display: block;
}
</style>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/08 00:44