vue.js - 如何让我的 v-show 在 Vue JS 中显示组件
问题描述
我试图仅在满足条件时才呈现组件。我对 没有问题v-if
,即组件仅在满足条件时才呈现。但我更喜欢使用v-show
(因为我预计用户会频繁切换)。
html有这样的东西
<Moe v-show="isMoe" />
<Larry />
<Curly />
脚本有这样的东西
import Moe from "@/components/Moe.vue";
import Larry from "@/components/Larry.vue";
import Curly from "@/components/Curly.vue";
export default {
data: function() {
return {
stooge: "Curly"
}
},
components, {
Moe,
Larry,
Curly
},
computed: {
isMoe() {
return this.stooge == "Moe"
}
}
}
附录
If this.stooge = "Curly"
, Moe 组件仍然渲染
解决方案
我能够通过将组件放置在 div 标签内并将 v-show 移动到父 div 上来解决问题。这是一个调整后的代码:
<div v-show="isMoe">
<Moe />
</div>
<Larry />
<Curly />
import Moe from "@/components/Moe.vue";
import Larry from "@/components/Larry.vue";
import Curly from "@/components/Curly.vue";
export default {
data: function() {
return {
stooge: "Curly"
}
},
components: {
Moe,
Larry,
Curly
},
computed: {
isMoe() {
return this.stooge === "Moe"
}
}
}
推荐阅读
- python - IndentationError:意外缩进 - 在字符串中使用变量
- json - 将多个列表列转换为pyspark数据框中的json数组列
- python - 使用 concurrent.futures 模块的 Python 线程
- kubernetes - OpenVPN-Client Pod on K8s - Local network unreachable
- buddypress - BuddyPress 个人资料页面的附加自定义页面构建挂钩
- python - 从列值显示散点图标题
- android - Jetpack Compose 现有多模块应用程序中的依赖项
- sql - 您是否需要全面验证数据库和应用程序中的数据?
- python - 在字符串 Python 中插入
- javascript - 几乎所有东西都试过了 - iFrame 总是 150px