《Vue3实战》 第六章 样式绑定和事件处理

news/2024/7/10 0:18:44 标签: Vue, 样式绑定, 事件

前言

Vue3的样式绑定事件处理

1、样式绑定

class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。

1.1、v-bind:class 可以简写为 :class

1.1.1、绑定对象

<template>
  <div class="static" :class="classObject"/>
</template>

<script>
export default {
  name: 'HelloWorld',
  data(){
        return{
            classObject:{
              'active':false,
              'text-danger':true
            }
        }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.static {
  width: 100px;
  height: 100px;
}
.active {
  background: green;
}
.text-danger {
  background: red;
}
</style>

1.1.2、绑定一个返回对象的计算属性

<template>
  <div class="static" :class="classObject"/>
</template>

<script>
export default {
  name: 'HelloWorld',
  data(){
        return{
            isActive: true,
            error: null
        }
  },
  computed:{
    classObject(){
      return {
        active: this.isActive && !this.error,
        'text-danger':this.error && this.error.type ==='fatal'
      }              
    }
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.static {
  width: 100px;
  height: 100px;
}
.active {
  background: green;
}
.text-danger {
  background: red;
}

h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

1.1.3、绑定数组

<template>
  <div class="static" :class="[activeClass,errorClass]"/>
</template>

<script>
export default {
  name: 'HelloWorld',
  data(){
        return{
            activeClass: 'active',
            errorClass: 'text-danger'
        }
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.static {
  width: 100px;
  height: 100px;
}
.active {
  background: green;
}
.text-danger {
  background: red;
}

h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

1.2、 v-bind:style

<template>
  <div :style="[baseStyles, overridingStyles]">张三</div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data(){
    return {
      baseStyles: {
              color: 'green',
              fontSize: '30px'
          },
        overridingStyles: {
              'font-weight': 'bold'
          }
      }
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.static {
  width: 100px;
  height: 100px;
}
.active {
  background: green;
}
.text-danger {
  background: red;
}

h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

2、事件处理

v-on 指令来监听 DOM 事件,从而执行 JavaScript 代码。
v-on 指令可以缩写为 @ 符号。

语法格式:

v-on:click=“methodName”

@click=“methodName”

2.1、例子1 接收定义的方法来调用

<template>
  <div>
    <button @click="greet">点我</button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data(){
    return{
      name:'zs'
    }
  },
  methods:{
    greet(event){
      alert("hello " + this.name);
      if(event){
        alert(event.target.tagName);
      }
    }
  }  
  
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.static {
  width: 100px;
  height: 100px;
}
.active {
  background: green;
}
.text-danger {
  background: red;
}

h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

2.2、事件处理程序中可以有多个方法,这些方法由逗号运算符分隔

<template>
  <div>
    <button @click="greet1(),greet2()">点我</button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data(){
    return{
      name:'zs'
    }
  },
  methods:{
    greet1(){
      alert("invoke1 " + this.name);
      
    },
    greet2(){
      alert("invoke2" + this.name);
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.static {
  width: 100px;
  height: 100px;
}
.active {
  background: green;
}
.text-danger {
  background: red;
}

h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

2.3、事件修饰符

Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:

event.preventDefault() 或 event.stopPropagation()。

Vuejs____340">2.3.1、Vue.js 通过由点 . 表示的指令后缀来调用修饰符。

  • .stop - 阻止冒泡
  • .prevent - 阻止默认事件
  • .capture - 阻止捕获
  • .self - 只监听触发该元素的事件
  • .once - 只触发一次
  • .left - 左键事件
  • .right - 右键事件
  • .middle - 中间滚轮事件
<!-- 阻止单击事件冒泡 -->
<a v-on:click.stop="doThis"></a>
<!-- 提交事件不再重载页面 -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- 修饰符可以串联  -->
<a v-on:click.stop.prevent="doThat"></a>
<!-- 只有修饰符 -->
<form v-on:submit.prevent></form>
<!-- 添加事件侦听器时使用事件捕获模式 -->
<div v-on:click.capture="doThis">...</div>
<!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 -->
<div v-on:click.self="doThat">...</div>
<!-- click 事件只能点击一次,2.1.4版本新增 -->
<a v-on:click.once="doThis"></a>

2.4、按键修饰符

@keyup.enter

2.4.1、全部的按键别名

  • .enter
  • .tab
  • .delete (捕获 “删除” 和 “退格” 键)
  • .esc
  • .space
  • .up
  • .down
  • .left
  • .right

2.4.2、系统修饰键

  • .ctrl
  • .alt
  • .shift
  • .meta

2.4.3、鼠标按钮修饰符

  • .left
  • .right
  • .middle

2.5、.exact 修饰符

.exact 修饰符允许你控制由精确的系统修饰符组合触发的事件

<!-- 即使 Alt 或 Shift 被一同按下时也会触发 -->
<button @click.ctrl="onClick">A</button>

<!-- 有且只有 Ctrl 被按下的时候才触发 -->
<button @click.ctrl.exact="onCtrlClick">A</button>

<!-- 没有任何系统修饰符被按下的时候才触发 -->
<button @click.exact="onClick">A</button>

http://www.niftyadmin.cn/n/232682.html

相关文章

计组2.4——加法器的设计

计组&#xff1a;2.4算术逻辑单元异或门实现奇偶校验的原理串行加法器&&并行加法器并行加法器的优化算术逻辑单元 控制信号&#xff1a; 当M0时表示算术运算 当M1时表示逻辑运算 S0~ S3表示做什么运算&#xff0c;因此ALU可以表示16种算数运算和16种逻辑运算 Ai,Bi代表…

关于改造维护工单BAPI_ALM_ORDER_MAINTAIN用于生产订单组件批量修改

1、研究背景 1.1、业务背景 由于销售、研发、工艺等需要频繁变更&#xff0c;导致工单中组件需要频繁的进行变更&#xff0c;修改组件的物料&#xff0c;数量&#xff0c;库存地点&#xff0c;工序等内容。 1.2、技术痛点 为了满足要求&#xff0c;使用了函数&#xff1a;CO…

重感知还是重地图?其实无需选择

近来&#xff0c;关于自动驾驶应该重感知还是重地图是个热点话题&#xff0c;很多重量级车厂、自动驾驶供应商都开始提出重感知轻地图的方案&#xff0c;并承诺很快能发布出对应的产品。业界也出现了高精地图已“死”等类似的言论。 一时之间&#xff0c;似乎轻地图已经成为了…

5G-OAI关于物理层中PDCCH源码解析

5G物理层是指5G网络的传输技术&#xff0c;包括无线帧、子帧、时隙、符号等方面的定义和规范。具体来说&#xff0c;5G物理层定义了无线帧的长度、帧结构、子帧结构、传输速率、带宽、时间同步等方面的参数&#xff0c;以及物理层信道的编码、调制和解调方式等方面的规范。5G物…

【大唐杯学习超快速入门】5G技术原理仿真教学——通信网络认知

这里写目录标题智能通信业务对讲机固定电话电视机wifiPAD扫地机器人手机电信业务号码办理基础业务办理业务选择通信流程模拟增值业务办理工程实践信号塔基站机房传输&核心机房智能通信业务 按照顺序来进行&#xff0c;对讲机&#xff0c;固定电话&#xff0c;电视机&#…

九龙证券|巴菲特又出手了!嗅到了什么?日本央行新行长发声

巴菲特或再发日元债&#xff0c;嗅到了什么&#xff1f; 有商场消息称&#xff0c;知情人士泄漏&#xff0c;沃伦巴菲特旗下伯克希尔哈撒韦已开端就出售日元债券向投资者征求意见。这似乎印证了稍早之前的信息&#xff0c;此前&#xff0c;据彭博报导&#xff0c;伯克希尔哈撒韦…

9、数据库学习规划:Oracle - 学习规划系列文章

甲骨文公司的Oracle数据库是笔者认为的目前市面上性能最强大的数据库。其版本也发展到了现在的12c&#xff0c;提供的功能也更加的强大了。以前笔者使用的是9i&#xff0c;十几年过去了&#xff0c;也才发展到12代&#xff0c;说明Oracle数据库的性能和底层技术是非常完善和强大…

Java中线程的常用操作-后台线程、自定义线程工厂ThreadFactpry、join加入一个线程、线程异常捕获

场景 Java中Thread类的常用API以及使用示例&#xff1a; Java中Thread类的常用API以及使用示例_霸道流氓气质的博客-CSDN博客 上面讲了Thread的常用API&#xff0c;下面记录下线程的一些常用操作。 注&#xff1a; 博客&#xff1a;霸道流氓气质的博客_CSDN博客-C#,架构之…