07-vue常用操作指令
阿新 • • 發佈:2021-01-21
技術標籤:vue框架學習和專案常見問題
常用操作指令:
v-bind
示例1:基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind</title>
</head>
<body>
<div id="app">
<img v-bind:src="imgUrl" alt="" >
<a href="http://www.baidu.com">百度一下</a>
<a :href="aHref">百度一下</a>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
imgUrl: 'https://cn.vuejs.org/images/logo.png' ,
aHref: 'http://www.baidu.com'
}
})
</script>
</html>
其中‘:’是v-bind的語法糖
示例2:動態繫結class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind動態繫結class</title>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h2 :class="{active: isActive}">hello vue</h2>
<button @click="switchColor">切換字型顏色</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
isActive: true
},
methods: {
switchColor: function () {
this.isActive = !this.isActive
}
}
})
</script>
</html>
示例3:動態繫結style
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-bind動態繫結style</title>
</head>
<body>
<div id="app">
<h2 :style="{fontSize: finalSize + 'px',color: finalColor}">{{message}}</h2>
<button @click="add">+</button>
<button @click="sub">-</button>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'hello Vue',
finalSize: 100,
finalColor: 'green',
},
methods: {
add: function () {
this.finalSize++
},
sub: function () {
this.finalSize--
}
}
})
</script>
</html>