这篇文章主要介绍了vue中的v-show,v-if,v-bind怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue中的v-show,v-if,v-bind怎么使用文章都会有所收获,下面我们一起来看看吧。
根据表达式的真假,切换元素的显示和隐藏如:广告,遮罩层等
<div id='app'>
<input type="button" value="切换显示状态" @click="changeIsshow">
<input type="button" value="增加年龄" @click="addage">
<img v-show="isshow" width="200px" height="200px"
src="https://cache.yisu.com/upload/information/20230406/112/69205.png" />
<img v-show="age>=18" width="200px" height="200px"
src="https://cache.yisu.com/upload/information/20230406/112/69205.png" />
</div>
<script>
Vue.config.productionTip = false //阻止vue在启动时生成生产提示。
var app = new Vue({
el: '#app',
data: {
isshow: false,
age: 17,
},
methods: {
changeIsshow: function () {
this.isshow = !this.isshow;
},
addage: function () {
this.age++;
}
},
})
</script>
根据表达式的真假,切换元素的显示和隐藏(操作dom元素),频繁操作用v-show,反之用v-if
<div id='app'>
<input type="button" value="显示/隐藏" @click="changehide">
<p v-if="isshow">卷完后端卷前端</p>
<p v-show="isshow">卷完后端卷前端-vshow</p>
</div>
<script>
Vue.config.productionTip = false //阻止vue在启动时生成生产提示。
var app = new Vue({
el: '#app',
data: {
isshow:false,
},
methods: {
changehide:function(){
this.isshow = !this.isshow;
}
},
})
</script>
设置元素的属性比如(src,title,class等)v-bind:属性名=表达式 v-bind:可简写成 :class="" 省掉v-bind
<style>
.active{
border: 1px solid red;
}
</style>
<div id='app'>
<img v-bind:src="imgsrc" width="200px" height="200px" alt=""/><br>
<img :src="imgsrc" width="200px" height="200px" alt="" :title="title+'!!!'" :class="isactive?'active':''" @click="togactive"/>
<img :src="imgsrc" width="200px" height="200px" alt="" :title="title+'!!!'" :class="{active:isactive}" @click="togactive"/>
</div>
<script>
Vue.config.productionTip = false //阻止vue在启动时生成生产提示。
var app = new Vue({
el: '#app',
data: {
imgsrc:"https://cache.yisu.com/upload/information/20230406/112/69205.png",
title:"vUE卷你",
isactive:false,
},
methods: {
togactive:function(){
this.isactive = !this.isactive;
}
},
})
</script>
关于“vue中的v-show,v-if,v-bind怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“vue中的v-show,v-if,v-bind怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.cnblogs.com/superip/p/17292154.html