主要内容:vue常用指令与语法基础
- 计算属性(computed)和过滤器(filters)
- 监听器属性(watch)
- 样式修改(dom中引入vue元素)
- 条件与显示(dom中加入v-if条件判断)
- 列表渲染(dom中通过v-for进行遍历及渲染)
1. vue模版语法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="../lib/vue.js"></script>
<title>模板语法</title>
</head>
<body>
<div class="app">
<p>{{words}}</p>
<!-- v-: vue指令的前缀,以html自定义属性的方式书写 -->
<p v-html="code"></p>
<p>{{10*30}}</p>
<p>{{true ? '心情好' : '难过'}}</p>
<p>{{'ABCD'.split('').reverse().join('')}}</p>
<p>{{'Hello World'}}</p>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
words: "Hello php.cn",
code: "<strong>朱老师</strong>",
},
});
</script>
</body>
</html>
2. 计算属性和过滤器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>计算属性和过滤器</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<div class="app">
<p>{{reverseWords}}</p>
<p>{{words | wordsToCase | substring}}</p>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
words: "hello php.cn",
},
// 计算属性
computed: {
reverseWords() {
return this.words.split("").reverse().join("");
// 先拆分、再反转、再合并
},
},
// 过滤器
filters: {
wordsToCase(str) {
return str.toUpperCase();
},
substring: (str) => str.substr(2, 3),
// substr:从第三位开始取三个元素
},
});
</script>
</body>
</html>
3. 监听器属性(watch)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>侦听器属性</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<p>小小加法器</p>
<div class="app">
<input type="number" v-model="add1" /> +
<input type="number" v-model="add2" /> = <span>{{res}}</span>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
add1: 0,
add2: 0,
res: 0,
},
// 侦听器属性
/*
watch: {
add1(newVal, oldVal) { // 这个地方等同于 add1: function(newVal, oldVal)......
// console.log(
// "new = %d, old = %d",
// parseFloat(newVal),
// parseFloat(oldVal)
// );
// console.log("new = %d, old = %d", newVal * 1, oldVal * 1);
this.res = this.newVal * 1 + this.oldVal * 1;
},
add2(newVal, oldVal) {
this.res = this.newVal * 1 + this.oldVal * 1;
},
},
*/
// 公共方法
methods: {
// 事件方法或公共函数
add(newVal, addend) {
this.res = this.newVal * 1 + this.oldVal * 1;
// 通过 *1的方式进行字符串转数字。
},
},
watch: {
add1(newVal) {
this.add(newVal, this.add2);
},
add2(newVal) {
this.add(newVal, this.add1);
},
},
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>侦听器属性2</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<p>小小加法器</p>
<div class="app">
<input type="number" v-model="add1" /> +
<input type="number" v-model="add2" /> = <span>{{res}}</span>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
add1: 0,
add2: 0,
},
computed: {
res() {
return this.add1 * 1 + this.add2 * 1;
},
},
});
</script>
</body>
</html>
4. 样式的修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>样式的修改</title>
<script src="../lib/vue.js"></script>
<style>
.active {
color: red;
}
.bigger {
font-size: 1.5rem;
}
</style>
</head>
<body>
<div class="app">
<!-- <p class="active">Hello World</p> -->
<!-- 对象字面量 -->
<!-- <p v-bind:class="{active: isActive, bigger: isBigger}">Hello World</p> -->
<!-- 对象字面量简化 -->
<!-- <p v-bind:class="style1">Hello World</p> -->
<!-- 数组 -->
<!-- <p v-bind:class="[active, bigger]">Hello World</p> -->
<!-- 字面量 -->
<!-- <p v-bind:class="['active', 'bigger']">Hello World</p> -->
<!-- <p style="color: red">Hello World</p> -->
<!-- v-bind:style="" -->
<!-- <p style="color: violet; font-size: 2rem">Hello World</p> -->
<!-- <p v-bind:style="`color: violet; font-size: 2rem`">Hello World</p> -->
<!-- <p v-bind:style="{color:color, fontSize:fontSize}">Hello World</p> -->
<p :style="{color:color, fontSize:fontSize}">Hello World</p>
<!-- 这个地方是简写,将v-bind省略掉了 -->
<p :class="'active'">php.cn</p>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
isActive: true,
isBigger: true,
style1: {
active: () => this.isActive,
bigger: () => this.isBigger,
},
active: "active",
bigger: "bigger",
// style
color: "green",
fontSize: "2rem",
},
});
</script>
</body>
</html>
5. 条件与显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>条件与显示</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<div class="app">
<p v-if="score > 90 && score <=100">学帝</p>
<p v-else-if="score >= 80 && score < 90">学霸</p>
<p v-else-if="score >= 60 && score < 80">学渣</p>
<p v-else>留级</p>
<hr />
<button type="button" v-on:click="handle">{{tips}}</button>
<p v-show="flag">vue是一个语法简洁的渐进式前端框架</p>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
score: 95,
flag: true,
tips: "隐藏",
},
methods: {
handle() {
this.flag = !this.flag;
// 上面这样设置后,等于click一次为显示,一次为隐藏。两者动态切换了。
this.tips = this.flag ? "隐藏" : "显示";
},
},
});
// if (){}: v-if
// if (){} else {} : v-else
// if (){} else if () {} else {}: v-else-if
</script>
</body>
</html>
6. 列表渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="../lib/vue.js"></script>
<title>列表渲染</title>
</head>
<body>
<div class="app">
<!-- v-for="item of arr" -->
<ul>
<!-- 数组 -->
<li v-for="color of colors">{{color}}</li>
</ul>
<hr />
<ul>
<!-- 对象-->
<li v-for=" (item,key) of course">{{key}} : {{item}}</li>
</ul>
<hr />
<ul>
<!-- 对象数组:每个元素师对象-->
<li v-for="user of users">{{user.name}} ( {{user.email}} )</li>
</ul>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
// array
colors: ["red", "green", "blue"],
// object
course: { name: "vue基础", lecture: "朱老师" },
// obj-arr
users: [
{ name: "admin", email: "a@qq.com" },
{ name: "peter", email: "p@qq.com" },
],
},
});
// 遍历数组
// const arr = ["html", "css", "js"];
// console.log(Object.keys(arr));
// Object.keys(arr).forEach((item) => console.log(arr[item]));
// 遍历对象
// const obj = { id: 1, name: "peter", email: "peter@qq.com" };
// Object.keys(obj).forEach((item) => console.log(obj[item]));
// 遍历对象数组
const objArr = [
{ id: 1, name: "admin" },
{ id: 2, name: "jack" },
];
console.log(Object.keys(objArr));
let lis = "";
// 或者
// for (item of objArr) {
// console.log(item.id, item.name);
// }
</script>
</body>
</html>