javascript - Vue js:如何隐藏按钮
问题描述
我是下面这段代码中 Vue js 的新手,我想在没有选择任何内容时隐藏按钮“清除过滤器”,仅在调用函数“selectedAnswer(index)”时显示按钮,以便它仅在应用过滤器时显示,否则它应该被隐藏,有没有办法在我的代码中做到这一点?并提前感谢
<template>
<div class="container" width=800px>
<b-row>
<b-col cols="8">
<h1> Recently Asked </h1>
<ul class="container-question" v-for="(question1,index) in questions" :key="index">
<li>
{{question1.question}}
</li>
</ul>
</b-col>
<b-button class="outline-primaryy" style="margin:auto;" @click="ClearFilter" >Clear Filter</b-button>
</div>
<router-view />
</div>
</template>
<script>
export default {
data() {
return {
questions: [],
answered: null,
index: 0,
selectedIndex: null,
}
},
methods: {
selectedAnswer(index) {
this.selectedIndex = index;
this.questions = this.questions.filter((question) => question.incorrect_answers.includes(index))
console.log(index)
},
ClearFilter() {
this.questions = this.unmutated
},
watch: {
question1: {
handler() {
this.selectedIndex = null;
this.answered = false;
},
},
},
},
mounted: function() {
fetch('https://opentdb.com/api.php?amount=10&category=9&difficulty=medium&type=multiple', {
method: 'get'
})
.then((response) => {
return response.json()
})
.then((jsonData) => {
this.questions = jsonData.results
this.unmutated = jsonData.results;
})
}
}
</script>
解决方案
您只需要将 a 添加v-if="selectedIndex"
到您的 btn 元素。IE
<b-button v-if="selectedIndex" class="outline-primaryy" style="margin:auto;" @click="ClearFilter" >Clear Filter</b-button
推荐阅读
- r - ggplot:如何选择高于某个阈值但仍显示正确百分比的数据
- google-app-engine - 谁启动了谷歌云调度程序?
- javascript - 为什么“in”不能用于检查特定单词是否在 JavaScript 的数组中
- reactjs - AWS chime 反应没有音频输入
- css - 边框图像源:线性渐变仅读取一种颜色
- javascript - 在 URL 动态更改上重新运行 Firefox 插件的脚本
- powershell - 将 VBScript 代码转换为 Power Shell 代码以将值写入 Windows 10 工作站的 Active Directory 计算机帐户描述
- time-complexity - 使用 ∈ 表示法比较时间复杂度
- angular - Angular - 从 ng-content 访问组件变量
- parameters - 在 CDK 'codepipeline_actions.CloudFormationCreateUpdateStackAction' 中使用部署时间参数