前端-vue基础
一、vue基础
1.vue的模板语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
{{ message }}
<br>
<input type="text" v-model="msg">
</div>
</body>
<script>
let app = new Vue({
el: "#app",
data : {
message : 'hello world',
msg : 'hello vue'
}
})
</script>
</html>
2.vue的基本语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
<style>
.one{
color: red;
}
.two{
color: blue;
}
</style>
</head>
<body>
<div id="app">
{{ message }}
<div v-html="message"></div>
<div v-text="message"></div>
<div v-bind:title="msg">DOM元素属性绑定的完整写法</div>
<div :title="msg">DOM元素属性绑定的简写</div>
<div :class="className1">
{{shop}}
</div>
<div :class="className2">
{{shop}}
</div>
</div>
</body>
<script>
let app = new Vue({
el: '#app',
data: {
message: '<span>hello world</span>',
msg : '你指向了这里',
shop : '水果',
className1 : 'one',
className2 : 'two'
}
})
</script>
</html>
3.绑定样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
<style>
.one {
color: red;
}
.two {
color: blue;
}
</style>
</head>
<body>
<div id="app">
<div :class="{one:oneActive,two:twoActive}">
1.{{ msg }}
</div>
<div :class="userId==1?className1:className2">
2.{{ msg }}
</div>
<div :style="{color:colorValue,fontSize:fontSizeValue}">
3.{{ msg }}
</div>
</div>
</body>
<script>
let app = new Vue({
el: '#app',
data: {
userId: 2,
oneActive: false,
twoActive: true,
msg: 'DOM元素的样式绑定',
className1: 'one',
className2: 'two',
colorValue: 'green',
fontSizeValue: '50px'
}
})
</script>
</html>
4.条件渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<div v-if="num==1">A</div>
<div v-else-if="num==2">B</div>
<div v-else-if="num==3">C</div>
<div v-else-if="num==4">D</div>
<div v-else="num==5">E</div>
<div v-if="isShow">这里是v-if</div>
<div v-show="isShow">这里是v-show</div>
</div>
</body>
<script>
let app = new Vue({
el : '#app',
data : {
num : 4,
isShow : true
}
})
</script>
</html>
5.事件处理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<button v-on:click="point">求点击</button>
<br><br>
<button @click="point">点击事件的简写</button>
<br><br><br>
<button @click="add(5)">+</button>
<button @click="reduce(3)">-</button>
<br>
<div>
{{ num }}
</div>
</div>
</body>
<script>
let app = new Vue({
el: '#app',
data: {
num: 10
},
methods: {
point() {
alert('你点击了按钮')
},
add(value) {
this.num += value
},
reduce(value) {
if (this.num <= value) {
this.num = 0
} else {
this.num -= value
}
}
},
})
</script>
</html>
6.循环遍历指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<p v-for="(value,key,index) in student">
{{ index + 1 }},{{ key }},{{ value }}
</p>
<p v-for="value in student">
{{ value }}
</p>
<br><br><br>
<div v-for="(item,index) in userArr">
{{ item.userId }},{{ item.userName }},{{ item.userSex }}
</div>
</div>
</body>
<script>
let app = new Vue({
el: '#app',
data: {
student : {
name : '张三',
age : 18,
sex : '男'
},
userArr : [
{
userId : 1,
userName : '小明',
userSex : '男'
},
{
userId : 2,
userName : '小红',
userSex : '女'
},
{
userId : 3,
userName : '小丽',
userSex : '女'
}
]
}
})
</script>
</html>
7.计算属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<p>当前num的值是 {{ num }} → 函数方法</p>
<div>当前num的值是 {{ msg }} → 计算属性方法 </div>
<button @click="change">改变num的值</button>
</div>
</body>
<script>
let app = new Vue({
el : '#app',
data : {
num : 1
},
methods: {
change(){
this.num = Math.floor(Math.random() * 2)
console.log('调用函数:' + this.num)
}
},
computed: {
msg(){
console.log('调用计算属性:' + this.num)
return this.num % 2 == 0 ? '偶数' : '奇数'
}
},
})
</script>
</html>