一.Vue是什么:
概念:Vue是一个用于构建用户界面的渐进式的框架
以下的内容是自里向外的
- 声明式渲染(Vuejs核心包)
- 组件系统(Vuejs核心包)
- 客户端路由VueRouter
- 大规模状态管理Vuex
- 构建工具Webpack/Vite
Vue的两种使用方式:
- Vue核心包开发->局部模块改造
- Vue核心包&Vue插件 工程化开发
二.创建Vue实例与初始化渲染
- 构建用户界面
- 创建Vue实例,初始化渲染
- 准备容器
- 引包-开发版本/生产版本
- 创建Vue new Vue();
- 指定配置项,渲染数据
- el指定挂载点 -#id
- data提供数据
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id="app">
<p>{
{
message }}</p>
<button v-on:click="changeMessage">改变消息</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: '这是一个简单的 Vue 实例'
},
methods: {
changeMessage: function () {
this.message = '消息已改变!';
}
}
});
</script>
</body>
</html>
三.Vue指令
Vue指令的定义:有v-前缀的标签属性
Vue根据不同的【指令】,针对【标签】实现不同的【功能】
<!--Vue指令: v-前缀的标签属性 -->
<div v-html="str"></div>
<!--普通标签属性-->
<div class="box"></div>
<div title="小张"></div>
1.v-html
插值表达式:{ {msg}} 其中msg是在data中定义的变量 vue会将其渲染到html标签中 而{ {msg}}插值表达式就在标签中
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id="app" v-html="msg">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
msg:`<a href="http://www.baidu.com">测试内容</a>`
}
})
</script>
</body>
</html>
- v-html=“data中的属性”
- data中的属性要用``作为模板语法表示
- v-html等价于js中的innerHTML
- data中的属性必须是一个完整的html标签
2.v-show与v-if
v-show(条件渲染)
- 作用:控制元素显示隐藏
- 语法:v-show=“表达式” 表达式的值true显示 false隐藏
- 原理:display:none(本质控制的css样式-适合频繁切换显示隐藏的场景)
v-if(判断条件)
- 作用:控制元素显示隐藏
- 语法:v-if=“表达式” 表达式值true显示 false隐藏
- 原理:基于条件判断,是否创建或者移除元素节点(适合不频繁的场景)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
<style>
.box {
display: flex; /* 使用 flexbox 布局 */
justify-content: center; /* 在主轴上居中 */
align-items: center; /* 在交叉轴上居中 */
height: 200px;
width: 200px;
text-align: center;
border-radius: 5px;
border: 2px solid black;
background-color: white;
padding: 10px;
margin: 10px;
box-shadow: none;
}
</style>
</head>
<body>
<div id ="app">
<div v-show="flag" class="box">我是v-show控制的盒子</div>
<div v-if="flag" class="box">我是v-if控制的盒子</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
flag:false
}
})
</script>
</body>
</html>
3.v-else v-else-if
- 作用:辅助v-if进行判断渲染
- v-else v-else-if=“表达式”
- 必须结合v-if使用
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<p v-if="gender==1">性别:男</p>
<p v-if="gender==2">性别:女</p>
<p v-if="score>=90">成绩评定为A等因为其>90</p>
<p v-else-if="score>=70">成绩评定为B等,因为其>70</p>
<p v-else-if="score>=60">成绩评定C等,因为其>60</p>
<p v-else>成绩评定D等,你的问题很大</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
gender:2,
score:89
}
})
</script>
</body>
</html>
上述为完整例子,p可以是任意标签,接下来抽取抽象概念
<p v-if="">xxx</p>
<p v-else-if="xxx">xxx</p>
<p v-else>xxx</p>
4.v-on基础
-
作用:注册事件 = 添加监听+提供处理逻辑
-
语法:
-
v-on:事件名=”内联语句“
-
v-on:事件名=“methods中的函数名”
-
v-on:可以用@替代
-
<button v-on:click="count++"> 点我自增 </button> <button @click-"count++"> 点我自增 </button>
-
实例一:内联语句
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<button v-on:click="count--">点我做减法</button>
<br>
<button v-on:click="count++">点我做加法</button>
<br>
<span>{
{count}}</span>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
count:0
}
})
</script>
</body>
</html>
实例二:methods函数
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<button @click="fn">切换显示隐藏</button>
<h1 v-show="isShow">测试内容</h1>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
isShow:true
},
methods:{
fn(){
this.isShow=!this.isShow
}
}
})
</script>
</body>
</html>
5.v-on调用传参
- 场景需求:点击按钮购买道具需要扣除一定的资金
- 点击按钮发生响应可以通过v-on:事件=”命令“来实现
- 但是,如何传递扣除资金的参数呢?我们引入v-on调用传参来实现
- v-on:click=“函数名(传入的参数)”
- 你是否会想:那我如果希望点击按钮传递input框的值呢?这个答案会在v-model中揭晓
售货机扣钱案例来学习调用传参
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
<style>
.box{
border: 2px solid black;
width: 200px;
height: 200px;
text-align: center;
}
</style>
</head>
<body>
<div id ="app">
<div class="box">
<h3>自动售货机</h3>
<button @click="buy(5)">可乐5元</button>
<button @click="buy(10)">咖啡10元</button>
</div>
<p>银行卡余额:{
{deposit}}元</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
deposit:5000
},
methods:{
buy(price){
this.deposit=this.deposit-price
window.alert("购买成功!您的余额是"+deposit);
}
}
})
</script>
</body>
</html>
6.v-bind
- 作用:动态的设置html的标签属性
- 语法: v-bind:属性名=“表达式”
- 省略语法:v-bind:src=“xxx” => :src=“xxx”
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<img v-bind:src="imgUrl">
<img :src="imgUrl">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
imgUrl: 'D:/desklop/OIP-C.jpg'
},
})
</script>
</body>
</html>
7.数组概念-切换图片案例
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<button v-on:click="index++">下一张</button>
<button v-on:click="index--">上一张</button>
<br>
<img v-bind:src="list[index]">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
index:0,
list:[
'D:/desklop/OIP-C.jpg',
'D:/desklop/testpng.png',
'D:/desklop/OIP-C.jpg',
'D:/desklop/testpng.png',
]
},
})
</script>
</body>
</html>
8.v-for
- 作用:基于数据循环,多次渲染整个元素
- 语法:v-for=“(item,index) in 数组” ps:item是每一项
- 大多数时候和
- 与
- 一起使用
<p v-for="...">我是一个内容</p>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<h3>水果店</h3>
<ul>
<li v-for="(item,index) in list">
{
{item}} --{
{index}}
</li>
<li v-for="item in list">
{
{item}}
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
list:['西瓜','苹果','鸭梨','榴莲']
},
})
</script>
</body>
</html>
9.v-for+v-on实战-书架
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
<h3>书架</h3>
<ul>
<li v-for="(item,index) in booklist" :key="item.id">
<span>{
{item.name}}</span>
<span>{
{item.author}}</span>
<button v-on:click="del(item.id)">删除</button>
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
booklist:[
{id:1,name:"《红楼梦》",author:'曹雪芹'},
{id:2,name:"《西游记》",author:'吴承恩'},
{id:3,name:"《水浒传》",author:'施耐庵'},
{id:4,name:"《三国演义》",author:'罗贯中'},
]
},
methods:{
del(id){
this.booklist = this.booklist.filter(item => item.id!==id)
}
}
})
</script>
</body>
</html>
这段代码是用来过滤掉
booklist
数组中id
与给定id
相同的元素。它使用了 JavaScript 中的数组filter
方法,该方法会创建一个新数组,其中包含通过指定函数测试的所有元素。在这里,指定的函数是一个箭头函数,它会检查每个元素的id
是否与给定的id
不相同,如果不相同,则保留该元素。所以最终item.id==id的部分都保留下来了
10.v-for:key
<ul>
<li v-for="(item,index) in booklist" :key="item.id">
<span>{
{item.name}}</span>
<span>{
{item.author}}</span>
<button v-on:click="del(item.id)">删除</button>
</li>
</ul>
- 语法:key属性=”唯一标识"
- 给列表项添加的唯一标识。便于Vue进行列表项的正确排序复用
如果不加key v-for默认行为是原地修改元素(就地复用)
给元素添加唯一标识,便于Vue进行列表的正确排序复用
- key只能是字符串或者数字类型
- key必须要有唯一性
- 推荐使用id作为key,不推荐使用index作为key(会变化不对应)
11.v-model
- 作用:给表单元素使用,双向数据绑定 ->可以快速获取或设置表单元素内容
- 语法:v-model=‘变量’
- 数据变化——>视图自动更新
- 视图变化——>数据自动更新
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
</head>
<body>
<div id ="app">
账户:<input type="text" v-model="username"><br><br>
密码:<input type="password" v-model="password"><br><br>
<button @click="login">登陆</button>
<button @click="reset">重置</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
username:"",
password:"",
},
methods:{
login(){
console.log(this.username,this.password)
},
reset(){
this.username="",
this.password=""
}
}
})
</script>
</body>
</html>
12.记事本项目
- 功能需求:
- 列表渲染
- 删除功能
- 添加功能
- 底部统计与清空
<!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" />
<link rel="stylesheet" href="./css/index.css" />
<title>记事本</title>
</head>
<body>
<!-- 主体区域 -->
<section id="app">
<!-- 输入框 -->
<header class="header">
<h1>小黑记事本</h1>
<input v-model="todoName" placeholder="请输入任务" class="new-todo" />
<button @click="add" class="add">添加任务</button>
</header>
<!-- 列表区域 -->
<section class="main">
<ul class="todo-list">
<li class="todo" v-for="(item, index) in list" :key="item.id">
<div class="view">
<span class="index">{
{ index + 1 }}.</span> <label>{
{ item.name }}</label>
<button @click="del(item.id)" class="destroy"></button>
</div>
</li>
</ul>
</section>
<!-- 统计和清空 → 如果没有任务了,底部隐藏掉 → v-show -->
<footer class="footer" v-show="list.length > 0">
<!-- 统计 -->
<span class="todo-count">合 计:<strong> {
{ list.length }} </strong></span>
<!-- 清空 -->
<button @click="clear" class="clear-completed">
清空任务
</button>
</footer>
</section>
<!-- 底部 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
// 添加功能
// 1. 通过 v-model 绑定 输入框 → 实时获取表单元素的内容
// 2. 点击按钮,进行新增,往数组最前面加 unshift
const app = new Vue({
el: '#app',
data: {
todoName: '',
list: [
{ id: 1, name: '跑步一公里' },
{ id: 2, name: '跳绳200个' },
{ id: 3, name: '游泳100米' },
]
},
methods: {
del (id) {
// console.log(id) => filter 保留所有不等于该 id 的项
this.list = this.list.filter(item => item.id !== id)
},
add () {
if (this.todoName.trim() === '') {
alert('请输入任务名称')
return
}
this.list.unshift({
id: +new Date(),
name: this.todoName
})
this.todoName = ''
},
clear () {
this.list = []
}
}
})
</script>
</body>
</html>
四:指令补充/计算属性/监听器
1.指令的修饰符
指令修饰符:通过"."指明的指令后缀,不同后缀封装了不同的处理操作->简化代码
-
按键修饰符
-
@keyup.enter ->键盘回车监听
-
<header class="header"> <h1>记事本</h1> <input @keyup.enter="add" v-model="todoName" placeholder="请输入任务" class="new-todo" /> <button @click="add" class="add">添加任务</button> </header> <!--输入框注册了事件 keyup按键弹起 enter为回车 实现在输入框内回车即可添加-->
我们思考v-on:keyup.enter的内在逻辑:=“”中的函数若空参,可以用e承接事件名,然后以事件来调用方法
<script> const app = new Vue({ el:'#app', data:{ username: '' }, methods:{ fn(e){ console.log(e); console.log("键盘回车时候触发",this.username) } } }) </script>
-
-
v-model修饰符
- v-model.trim ->去除首尾空格
- v-model.number->转数字
-
事件修饰符
- @事件名.stop ->阻止冒泡
- 存在两个div嵌套,点击内部div命令会执行外部div方法与内部div方法,使用stop可以避免这种现象
- @事件名.prevent ->阻止默认行为
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>简单的 Vue 实例</title> <style> .father{ width: 200px; height: 200px; background-color: aqua; } .son{ width: 50px; height: 50px; background-color: blanchedalmond; } </style> </head> <body> <div id="app"> <h3>v-model修饰符.trim .number</h3> 姓名:<input v-model.trim="username" type="text"><br> 年龄:<input v-model.number="age" type="text"><br> <h3>@事件名.stop ->阻止冒泡</h3> <div @click.stop ="fatherFn" class="father"> <div @click.stop="sonFn" class="son">儿子</div> </div> <h3>@事件名.prevent ->阻止默认行为</h3> <a @click.prevent href="https://www.baidu.com">阻止默认行为</a> <!--在被.prevent修饰后 无法跳转链接 因为herf是a的一个默认行为--> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> const app = new Vue({ el:'#app', data:{ username: '', age:'', }, methods:{ fatherFn(){ alert("父亲被点击了") }, sonFn(){ alert("儿子被点击了") } } }) </script> </body> </html>
- @事件名.stop ->阻止冒泡
2.v-bind操作class(样式控制增强)
语法:v-bind=“对象/数组”
-
对象->键就是类名,值是布尔值。如果值是true,有这个类,否则无这个类。第一类特征在于v-bind修饰的就是class本身,而非是class中的某个具体属性。在于决定某个class是否被启用
<div class="box" v-bind:class="{类名1:布尔值,类名2:布尔值}"></div>
<a :class="{active:index===activeIndex}" href="#">{ {item.name}}
-
数组->数组中的所有类,都会添加到盒子上,本质是一个class列表.第二类是有特征的:第一个class样式是不绑定vue的,只有这个class里面的某个具体属性会绑定到vue。在于决定已经存在的class中的具体属性修改
<div class="box" v-bind:class="{类名1,类名2,类名3}"
<div class="inner" v-bind:style="{width:percent + '%'}">
<div class:"box" :style="{width:'400px',height:'400px',background:'green'}"
A.导航栏案例->v-bind应用
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
<style>
*{
margin: 0;
padding: 0;
}
ul{
display: flex;
border-bottom: 2px solid #e01222;
padding: 0 10px;
}
li {
width: 100px;
height: 50px;
line-height: 50px;
list-style: none;
text-align: center;
}
li a{
display: block;
text-decoration: none;
font-weight: bold;
color:#333333;
}
li a.active{
background-color: #e01222;
color: #fff;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in list" v-bind:key="item.id" @click="activeIndex = index">
<a :class="{active:index===activeIndex}" href="#">{
{item.name}}
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
activeIndex:1,
list:[
{Id:1,name:'秒杀窗口'},
{id:2,name:'每日特价'},
{id:3,name:'品类秒杀'}
]
}
})
</script>
</body>
</html>
B.进度条案例-v-bind绑定样式
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的 Vue 实例</title>
<style>
.progress{
height: 25px;
width: 400px;
border-radius: 15px;
background-color: #272425;
border: 3px solid #272425;
box-sizing: border-box;
margin-bottom: 30px;
}
.inner{
width: 50%;
height: 20px;
border-radius: 10px;
text-align: right;
position: relative;
background-color: #409eff;
background-size: 20px,20px;
box-sizing: border-box;
transition: all 1s;
}
.inner span{
position: absolute;
right: -20px;
bottom: -25px;
}
</style>
</head>
<!--
.inner中的 transition:all 1s是丝滑变化的原理
-->
<body>
<div id="app">
<div class="progress">
<div class="inner" v-bind:style="{width:percent + '%'}"><!--这里必须把%引用 因为width后面要接一个数值-->
<span>{
{percent}}</span>
</div>
</div>
<button @click="percent = 25">设置25%</button>
<button @click="percent=50">设置50%</button>
<button @click="percent = 75">设置75%</button>
<button @click="percent =100">设置100%</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
percent: 30
}
})
</script>
</body>
</html>
3.v-model应用其他表单元素
作用:获取/设置表单元素的值
v-model会根据控件类型自动选择正确的方法来更新元素
input:text ->value
textarea ->value
intput:checkbox ->checked
input:radio ->checked
select ->value
前置理解:
1. name: 给单选框加上 name 属性 可以分组 → 同一组互相会互斥
2. value: 给单选框加上 value 属性,用于提交给后台的数据
结合 Vue 使用 → v-model
前置理解:
1. option 需要设置 value 值,提交给后台
2. select 的 value 值,关联了选中的 option 的 value 值
结合 Vue 使用 → v-model
<!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>
<style>
textare{
display: block;
width: 240px;
height: 100px;
margin: 10px 0;
}
</style>
</head>
<body>
<div id="app">
<h1>学习网站</h1>
姓名:
<input type="text" v-model="username">
<br><br>
是否单身:
<input type="checkbox" v-model="isSingle">
<br><br>
<!--
前置理解:
1. name: 给单选框加上 name 属性 可以分组 → 同一组互相会互斥
2. value: 给单选框加上 value 属性,用于提交给后台的数据
结合 Vue 使用 → v-model
-->
性别:
<input v-model="gender" type="radio" name="gender" value="1">男
<input v-model="gender" type="radio" name="gender" value="2">女
<br><br>
<!--
前置理解:
1. option 需要设置 value 值,提交给后台
2. select 的 value 值,关联了选中的 option 的 value 值
结合 Vue 使用 → v-model
-->
所在城市:
<select v-model="cityId">
<option value="101">北京</option>
<option value="102">上海</option>
<option value="103">成都</option>
<option value="104">南京</option>
</select>
<br><br>
<button>立刻注册</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
username:'',
isSingle:false,
gender:"2",
cityId:'102',
desc:""
}
})
</script>
</body>
</html>
4.计算属性computed
概念:基于现有的数据,计算出来的新属性。依赖的数据变化,自动重新计算。
语法:
1.声明在computed配置项中,一个计算属性对应一个函数
2.使用起来和普通属性一样 { {计算属性名}}
计算属性->可以将一段求值的代码进行封装
3.如果在computed写了 xxx()这个方法 你就可以在html中{ {xxx}} 至于xxx到底是怎么来的 交给xxx() 这里通常是this来调用data中的值+回调函数进行计算
<!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>
<style>
table {
border: 1px solid #000;
text-align: center;
width: 240px;
}
th,td{
border: 1px solid #000;
}
h3{
position: relative;
}
</style>
</head>
<body>
<div id="app">
<h1>礼物清单</h1>
<table>
<tr>
<th>名字</th>
<th>数量</th>
</tr>
<tr v-for="(item,index) in list" :key="item.id">
<td>{
{item.name}}</td>
<td>{
{item.num}}</td>
</tr>
</table>
<p>礼物总数 {
{totalCount}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
list:[
{id:1,name:'篮球',num:1},
{id:2,name:'玩具',num:2},
{id:3,name:'铅笔',num:5},
]
},
computed:{
totalCount(){
// 基于现有的数据,编写求值逻辑
// 计算属性函数内部,可以直接通过 this 访问到 app 实例
// console.log(this.list)
// 需求:对 this.list 数组里面的 num 进行求和 → reduce
let total=this.list.reduce((sum,item)=>sum+item.num,0)
return total;
/*this.list 是一个数组,其中包含了一些对象或元素。
reduce 方法是数组对象的一个高阶函数,用于将数组中的每个元素执行一个指定的回调函数,并将结果汇总成单个值。在这个例子中,reduce 会对 this.list 数组中的每个元素执行回调函数。
回调函数接受两个参数,第一个参数是累积值(通常命名为 accumulator,在这里命名为 sum),第二个参数是当前正在处理的元素(在这里命名为 item)。
回调函数执行的操作是将当前元素的 num 属性值加到累积值上。
初始值为 0,作为累积值的起始值。*/
}
}
})
</script>
</body>
</html>
5.computed计算属性vs方法methods
computed计算属性:
**作用:**封装了一段对于数据的处理,求得一个结果
语法:
1.写在computed的配置项中
2.作为属性,直接使用->this.计算属性{ {计算属性}}
methods方法:
作用:给实例提供一个方法,调用以处理业务逻辑
语法:
1.写在methods配置项中
2.作为方法,需要调用 —> this.方法名() { { 方法名()}} @事件名=“方法名”
八股部分:
计算属性会对计算出来的结果缓存,再次使用直接读取缓存
依赖项变化了,会自动重新计算 ->并且再次缓存 (缓存特性-提升性能)
6.计算属性的完整写法
计算属性默认的简写,只能访问读取,不能修改
如果要修改,需要写计算属性的完整写法
一种常用逻辑:
1.Input中v-model绑定一个变量
2.展示部分以计算属性表示{ {xxx}}
3.定义动态属性:xxx的get和set方法:return xxx (这里的xxx是data中的属性xxx)
以上操作完成就可以实现输入框的内容实时反馈到标签表现中
computed:{
计算属性名(){
代码逻辑(计算逻辑)
return 结果
}
}
computed:{
计算属性名:{
get(){
一段代码逻辑(计算逻辑)
return 结果
},
set(修改的值)
一段代码逻辑(修改逻辑)
}
<!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>
<style>
input{
width: 30px;
}
</style>
</head>
<body>
<div id="app">
姓 <input type="text" v-model="firstName">+
名 <input type="text" v-model="lastName">=
<span>{
{fullName}}</span><br><br>
<button @click="changeName">改名卡</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
firstName:'刘',
lastName:'备',
},
methods:{
changeName(){
this.fullName = '黄忠'
}
},
computed:{
fullName:{
get(){
return this.firstName+this.lastName
},
set(value){
this.firstName = value.slice(0,1)
this.lastName=value.slice(1)
}
}
}
})
</script>
</body>
</html>
7.成绩案例
要点分析:
-
计算属性中的方法
-
this.list.reduce((sum,item)=>sum+list.score,0) //是指 用sum来储存累加变量 item表示当前处理的数组元素 类似于增强for中定义的第三方变量 0表示从0开始积累 //这里是在对list数组中每个元素的score进行累加
-
-
-
函数中的方法
-
this.list=this.list.filter(item=>item.id!==id) //非常经典的删除行 //filter实质上会生成一个新的数组,这个数组的元素是满足 第三方变量的id不等于id,也就是说 我传入的id不会被添加到新的数组中
-
this.list.unshift({ id:+new Date(), subject : this.subject, score: this.score }) this.subject='', this.score='' //list.unshift作用是:修改数组更新视图 说白了就是往[]中添加一个{} //这里是用作<tr>的v-for中的元素 //id用时间戳来代替 并且在标签中设定为:key //subject和score都取data中的 最后记得重置为空
-
<!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" />
<link rel="stylesheet" href="./styles/index.css" />
<title>Document</title>
</head>
<body>
<div id="app" class="score-case">
<div class="table">
<table>
<thead>
<tr>
<th>编号</th>
<th>科目</th>
<th>成绩</th>
<th>操作</th>
</tr>
</thead>
<tbody v-if="list.length>0">
<tr v-for="(item,index) in list" v-bind:key="item.id">
<td>{
{index+1}}</td>
<td>{
{item.subject}}</td>
<td :class="{red:item.score<60}">{
{item.score}}</td>
<td><a @click.prevent="del(item.id)" href="https://www.baidu.com">删除</td>
</tr>
</tbody>
<tbody v-else>
<tr>
<td colspan="5">
<span>总分:{
{totalScore}}</span>
<span style="margin-left: 50px">平均分:{
{averageScore}}</span>
</td>
</tr>
</tbody>
</table>
</div>
<div class="form">
<div class="form-item">
<div class="label">科目:</div>
<div class="input">
<input type="text"
placeholder="请输入科目"
v-model.trim="subject"/>
</div>
</div>
<div class="form-item">
<div class="label">分数:</div>
<div class="input">
<input type="text" placeholder="请输入分数"
v-model.number="score">
</div>
</div>
<div class="form-item">
<div class="label"></div>
<div class="input">
<button @click="add" class="submit">添加</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
list:[
{id:1,subject:'语文',score:62},
{id:7,subject:'数学',score:89},
{id:12,subject:'英语',score:70},
],
subject:'',
score:''
},
computed:{
totalScore(){
return this.list.reduce((sum,item)=>sum+list.score,0)
},
averageScore(){
if(this.list.length==0){
return 0
}
return (this.totalScore/this.list.length).toFixed(2)
}
},
methods:{
del(id){
this.list=this.list.filter(item=>item.id!==id)
},
add(){
if(!this.subject){
alert("请输入科目")
return
}
if(typeof this.score!=='number'){