1、v-bind基本使用
<div id="app">
<img v-bind:src="url" alt="美女">
<a :href="aHalf">百度一下</a>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
url: 'https://gd4.alicdn.com/imgextra/i3/0/O1CN01A4OaL02FU0TNaOcHQ_!!0-item_pic.jpg',
aHalf: 'https://www.baidu.com'
}
})
</script>
2、v-bind动态绑定class
<style>
.active{
color: red;
}
</style>
<div id="app">
<h1 :class="{active: active,line:line}">{{message}}</h1>
<h1 :class="getClass()">{{message}}</h1>
<button @click="change">点击换色</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
active:true,
line:true
},
methods: {
change: function (){
this.active = !this.active;
},
getClass: function (){
return {active: this.active,line: this.line}
}
}
})
</script>
3、v-for和v-bind的结合
<style>
.setRed {
color: red;
}
</style>
<div id="app">
<ul>
<li v-for="(item,index) in movies" :class="{setRed: index == current}" @click="setColor(index)">{{item}}</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello',
movies:['海尔兄弟','西游记','龙争虎斗个','三国演义'],
current:0
},
methods: {
setColor: function (index){
this.current = index;
}
}
})
</script>
4、v-bind绑定style
<div id="app">
<h2 :style=getStyle()>{{message}}</h2>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello'
},
methods: {
getStyle: function () {
return {
fontSize: '50px',
color: 'red'
}
}
}
})
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!