私が Vue 2+ を理解している限り、スロットのコンテンツが変更されたときにコンポーネントを再レンダリングする必要があります。私の場合、error-message
表示するスロット コンテンツが表示されるまで非表示にするコンポーネントがありました。最初に、このメソッドをv-if
コンポーネントのルート要素にアタッチしました (computed
プロパティは機能しません。Vue には反応性がないようthis.$slots
です)。
checkForSlotContent() {
let checkForContent = (hasContent, node) => {
return hasContent || node.tag || (node.text && node.text.trim());
}
return this.$slots.default && this.$slots.default.reduce(checkForContent, false);
},
これは、DOM 要素の追加または削除を含め、99% の変更がスロットで発生する場合はいつでもうまく機能します。唯一のエッジケースは、次のような使用法でした:
<error-message> {{someErrorStringVariable}} </error-message>
ここで更新されているのはテキスト ノードのみです。理由はまだ不明ですが、メソッドは起動しません。beforeUpdate()
このケースをandにフックして修正created()
し、完全な解決策としてこれを残しました。
<script>
export default {
data() {
return {
hasSlotContent: false,
}
},
methods: {
checkForSlotContent() {
let checkForContent = (hasContent, node) => {
return hasContent || node.tag || (node.text && node.text.trim());
}
return this.$slots.default && this.$slots.default.reduce(checkForContent, false);
},
},
beforeUpdate() {
this.hasSlotContent = this.checkForSlotContent();
},
created() {
this.hasSlotContent = this.checkForSlotContent();
}
};
</script>