赞
踩
通过 "." 指明一些指令 后缀,不同 后缀 封装了不同的处理操作 → 简化代码
为了方便开发者进行样式控制, Vue 扩展了 v-bind 的语法,可以针对 class 类名 和 style 行内样式 进行控制 。
v-bind 对于样式控制的增强 - 操作class
语法 :class = "对象/数组"
v-bind 对于样式控制的增强 - 操作style
语法 :style = "样式对象"
常见的表单元素都可以用 v-model 绑定关联 → 快速 获取 或 设置 表单元素的值
它会根据 控件类型 自动选取 正确的方法 来更新元素
简单来说就是使用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>
- textarea {
- display: block;
- width: 240px;
- height: 100px;
- margin: 10px 0;
- }
- </style>
- </head>
- <body>
-
- <div id="app">
- <h3>小黑学习网</h3>
-
- 姓名:
- <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="sex" type="radio" name="sex" value="0">男
- <input v-model="sex"type="radio" name="sex" value="1">女
- <br><br>
-
- <!--
- 前置理解:
- 1. option 需要设置 value 值,提交给后台
- 2. select 的 value 值,关联了选中的 option 的 value 值
- 结合 Vue 使用 → v-model
- -->
- 所在城市:
- <select v-model="cityId">
- <option value="100">北京</option>
- <option value="101">上海</option>
- <option value="102">成都</option>
- <option value="103">南京</option>
- </select>
- <br><br>
-
- 自我描述:
- <textarea v-model="description"></textarea>
-
- <button>立即注册</button>
- </div>
- <script src="./vue.js"></script>
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- username: '',
- isSingle: true,
- sex: '1',
- cityId: '102',
- description: ''
- }
- })
- </script>
- </body>
- </html>

概念:基于现有的数据,计算出来的新属性。 依赖的数据变化,自动重新计算。
语法:
① 声明在 computed 配置项中,一个计算属性对应一个函数
② 使用起来和普通属性一样使用 {{ 计算属性名 }}
计算属性 → 可以将一段 求值的代码 进行封装
- <!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" :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="#">删除</a></td>
- </tr>
-
- </tbody>
- <tbody v-else>
- <tr>
- <td colspan="5">
- <span class="none">暂无数据</span>
- </td>
- </tr>
- </tbody>
-
- <tfoot>
- <tr>
- <td colspan="5">
- <span>总分:{{total}}</span>
- <span style="margin-left: 50px">平均分:{{avg}}</span>
- </td>
- </tr>
- </tfoot>
- </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 class="submit" @click="add()">添加</button>
- </div>
- </div>
- </div>
- </div>
- <script src="../vue.js"></script>
-
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- list: [
- { id: 1, subject: '语文', score: 20 },
- { id: 7, subject: '数学', score: 99 },
- { id: 12, subject: '英语', score: 70 },
- ],
- subject: '',
- score: ''
- },
- computed: {
- total(){
- // 使用数组求和函数
- return this.list.reduce((sum,item)=>sum+item.score,0)
- },
- avg(){
- if(this.list.length === 0){
- return 0
- }
- return (this.list.reduce((sum,item)=>sum+item.score,0)/this.list.length).toFixed(2)
- }
- },
- methods: {
- del(id){
- this.list = this.list.filter(item=>id!==item.id)
- },
- add(){
- if(!this.subject){
- alert('请输入科目名称!')
- return
- }
- console.log( typeof this.score );
- if( typeof this.score !=='number' || !(this.score >= 0 && this.score <=100)){
- alert('你输入的不是数字,或者分数不在0-100之间!')
- return
- }
- this.list.unshift({ id: +new Date(), subject: this.subject, score: this.score })
- }
- }
- })
- </script>
- </body>
- </html>

作用:监视数据变化,执行一些 业务逻辑 或 异步操作。
语法:
① 简单写法 → 简单类型数据,直接监视
② 完整写法 → 添加额外配置项
小结:
watch侦听器的语法有几种?
① 简单写法 → 监视简单类型的变化
② 完整写法 → 添加额外的配置项 (深度监视复杂类型,立刻执行)
- <!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/inputnumber.css" />
- <link rel="stylesheet" href="./css/index.css" />
- <title>购物车</title>
- </head>
- <body>
- <div class="app-container" id="app">
- <!-- 顶部banner -->
- <div class="banner-box"><img src="./img/fruit.jpg" alt="" /></div>
- <!-- 面包屑 -->
- <div class="breadcrumb">
- <span>声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/96318推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。