Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
生产环境版本,优化了尺寸和速度
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
开发环境版本,包含了有帮助的命令行警告
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
挂载点:声明的一个区域
插值:插值用两个花括号包含,插值就是一个占位符
响应式:数据和 DOM 已经被建立了关联,所有东西都是响应式的。
//div包含的区域为挂载点
<div class="app">
//插值
{{ message }}
</div>
//创建一个Vue实例
const vm = new Vue({
//el声明挂载点为.app的类
el: ".app",
// 数据注入
data: {
message: "hello world!",
},
});
控制台修改了vm.massges的值,dom内的数据随之改变
<p v-text="message"></p>
<p v-once>{{world}}</p>
<p v-html="age"></p>
data: {
age: '<span style="background:red">28</span>',
},
<p :style="style">{{message}}</p>
data: {
message: "hello world!",
style: "color:red",
},
<input type="button" value="按钮1" @click="show()" />
methods: {
show() {
alert(1);
},
},
//事件对象的参数名必须是 $event
<button @click.stop="handle($event, 100, 200)">sum</button>
//handle(参数的顺序和数量必须一致)
handle(ev, a, b) {
console.log((a, b, a + b));
},
v-model指令用来在input、select、text、checkbox、radio等表单控件或者组件上创建双向绑定。
<div class="app">
<p>{{username}}</p>
<p><input type="text" v-model.lazy="username" /></p>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
username: "jack",
age: '18',
},
});
</script>
<li v-for="(value,index) in arr">{{index}}-{{value}}</li>
arr: ["apple", "banana", "orange", "pear"],
<li v-for="(value,prop,index) in perpson" :key="prop">{{index}}-{{prop}}-{{value}}</li>
perpson: {
username: "jack",
age: "18",
},
<li v-for="(city,index) in citys" :key="city.id">{{city.id}}-{{city.name}}</li>
citys: [
{ id: 1, name: "beijing" },
{ id: 2, name: "shanghai" },
{ id: 3, name: "tianjin" },
],
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
ul li {
list-style: none;
}
h2 {
text-align: center;
}
.app {
display: grid;
width: 20em;
}
.app ul:first-of-type {
background: red;
color: #fff;
display: grid;
grid-template-columns: repeat(4, 1fr);
justify-items: center;
align-items: center;
padding: 1em;
}
.app ul {
display: grid;
grid-template-columns: repeat(4, 1fr);
justify-items: center;
align-items: center;
padding: 0.5em;
}
.app ul:nth-child(odd) {
background: #ccc;
}
</style>
</head>
<body>
<div class="app">
<h2>用户信息表</h2>
<ul>
<li>姓名</li>
<li>年龄</li>
<li>性别</li>
<li>城市</li>
</ul>
<ul v-for="user in perpson" :key="user.id">
<li>{{user.name}}</li>
<li>{{user.age}}</li>
<li>{{user.gender}}</li>
<li>{{user.city}}</li>
</ul>
</div>
<script>
const vm = new Vue({
el: ".app",
data: {
perpson: [
{id: 1, name: "张三", age: 18, gender: "男", city: "北京" },
{id: 2, name: "赵四", age: 30, gender: "女", city: "天津" },
{id: 3, name: "王五", age: 21, gender: "男", city: "上海" },
{id: 4, name: "赵六", age: 15, gender: "女", city: "成都" },
],
},
});
</script>
</body>
</html>