logo头像

前端从未如此简单有趣

Vue01-基础语法-模板语法-基本指令-v-bind

本文于 604 天之前发表,文中内容可能已经过时。

Vue的引入方法

  • CDN引入
  • 本地引入

Vue动态的数据展示

  • 声明式编程 面试题:声明式编程和命令式编程的区别?

原生DOM都是采用的命令式编程,操作页面的元素都要获取该元素的DOM。然而在Vue中不需要操作原生的DOM,直接在data中声明数据即可。

  • 不直接操作DOM,将一些数据直接渲染到页面上

image-20240721130626365

Vue展示列表数据

  • 数组如何渲染
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script>
let title = "hello world"
const app = Vue.createApp({
template:`
<h1>{{ name }}</h1>
<ul>
<li v-for="(item,index) in name">{{item}}</li>
</ul>
`,
data:function () {
return {
title:title,
name: ["张三", "李四", "王五"],
};
}
})
console.log(app)
// 挂载
app.mount("#app")
</script>

Vue实现计数器【经典案例】

Option-api

  • data 面试题:为什么data是一个函数?
  • methods

Mystache双括号语法【大胡子语法】

Vue的指令

  • v-once
  • v-text
  • v-html
  • v-pre
  • v-cloak
  • v-memo

v-bind指令

v-bind绑定基本属性

需要动态的改变页面的属性,通过v-bind来动态的绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<script src="./lib/Vue.js"></script>
<style>
</style>
</head>
<body>
<h2>你好</h2>
<!-- 但是,除了内容需要动态来决定外,某些属性我们也希望动态来绑定。-->
<div id="app">
<div>
<h1>{{name}}</h1>
<img :src="url" alt="">
<!-- 有很多的,比如图片的链接src、网站的链接href、动态绑定一些类、样式等等 -->
<a :href="href">百度一下</a>
</div>
<button @click="update">改变</button>
</div>
<script>
const app = Vue.createApp({
data() {
return {
name:"chenyi",
url:"https://www.neu.edu.cn/images/dangdaihuinew.jpg",
href:"https://www.neu.edu.cn/",
url2:"https://www.neu.edu.cn/images/2696de7a-6d24-4272-a7b2-7466a35d17d4.jpg",
}
},
methods:{
update:function (){
this.href="https://cysir.icu";
this.url = this.url === this.url2 ? this.url : this.url2
}
}
});
console.log(app);
// 挂载
app.mount('#app');
</script>
</body>
</html>


v-bind绑定class属性

需要动态的改变页面的样式,通过v-bind来动态的绑定样式。【不同的写法】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<script src="./lib/Vue.js"></script>
<style>
.active{
color:red;
}
.demo{
font-size: 30px;
}
</style>
</head>
<body>

<div id="app">
<h1 >hello world</h1>
<!-- <button :class="isActive ? 'active':'' " @click="btnClick">按钮</button>-->

<!-- 动态class可以写对象语法-->
<!-- 我们可以传给:class (v-bind:class 的简写)一个对象,以动态地切换 class-->
<!-- <button :class="{active:isActive}" @click="btnClick" >按钮</button>-->

<!-- 对象语法的多个键值对-->
<!-- <button :class="{active:isActive,why:true}" @click="btnClick" >按钮</button>-->


<!-- 动态绑定的class是可以和普通的class同时的使用 ~!!!!!!!!!!!!!!!!!!-->
<button class="demo" :class="getDynamicClass()" @click="btnClick" >按钮</button>

<h1 :class="['abc','def']">hello</h1>
<h1 :class="['abc',className,getDynamicClass()]">hello</h1>

</div>
<script>
const app = Vue.createApp({
data() {
return {
isActive:false,
className:'why'
}
},
methods:{
btnClick:function (){
this.isActive = !this.isActive
},
getDynamicClass:function (){
return {active:this.isActive,why:true}
}
}
});

app.mount('#app');
</script>
</body>
</html>


v-bind绑定style属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<script src="./lib/Vue.js"></script>
<style>
.active{
color:red;
}
.demo{
font-size: 30px;
font-family: "Agency FB";
}
</style>
</head>
<body>

<div id="app">
<!-- 2.1.动态绑定style,在后面跟上:对象类型-->
<!-- 单引号 或者 驼峰-->
<!-- CSS property 名可以用驼峰式(camelCase)或短横线分隔(kebab-case, 记得用引号括起来)来命名;-->
<h1 :style="{color:fontColor,fontSize:fontSize}">hello world</h1>
<!-- 动态的绑定属性,这个属性是一个对象-->
<h2 :style="objStyle">chenyi</h2>
<!-- 动态的绑定属性,这个属性是一个数组-->
<h2 :style="[objStyle,{fontFamily:'Agency FB'}]">呵呵哈哈哈</h2>
</div>
<script>
const app = Vue.createApp({
data() {
return {
fontColor:"green",
fontSize:'60px',
objStyle:{
color:'green',
fontSize: '100px'
}
}
},
methods:{
btnClick:function (){
this.isActive = !this.isActive
},
getDynamicClass:function (){
return {active:this.isActive,why:true}
}
}
});

app.mount('#app');
</script>
</body>
</html>


v-bind绑定属性名

v-bind直接绑定对象

一般在组件传值的时候可以用