Vue学习之旅——事件处理、事件处理和组件基础

事件处理

监听事件

我们可以使用v-on指令(通常缩写为@符号)来监听DOM事件,并在触发事件时执行一些JavaScript

用法为v-on:click="methodName”或使用快捷方式@click="methodName"

<template>
  <button v-on:click="cnt += 1">cnt = {{cnt}}</button>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      cnt: 0
    }
  }
}
</script>

在这里插入图片描述

事件处理方法

然而许多事件处理逻辑会更为复杂,所以直接把JavaScript代码写在v-on指令中是不可行的。因此v-on还可以接收一个需要调用的方法名称。

<template>
  <button v-on:click="cnt += 1">cnt = {{cnt}}</button>
  <button v-on:click="add">cnt = {{cnt}}</button>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      cnt: 0
    }
  },
  methods: {
    add() {
      this.cnt += 1
    }
  }
}
</script>

内联处理器中的方法

可以直接叫他 “事件传递参数”。

<template>
  <button @click="cout('hello')">cout</button>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      cnt: 0
    }
  },
  methods: {
    cout(x) {
      alert(x)
    }
  }
}
</script>

在这里插入图片描述

表单输入绑定

你可以用v-model指令在表单<input><textarea><select>元素上创建双向数据绑定(即可获取又可设置)。

它会根据控件类型自动选取正确的方法来更新元素。尽管有些神奇,但v-model本质上不过是语法糖。

它负责监听用户的输入事件来更新数据,并在某种极端场景下进行一些特殊处理。

<template>
  <input v-model="cnt">
  <p>{{cnt}}</p>
  <button @click="cout">get</button>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      cnt: 0
    }
  },
  methods: {
    cout() {
      alert(this.cnt)
    }
  }
}
</script>

在这里插入图片描述

修饰符

.lazy

在默认情况下,v-model在每次input事件触发后将输入框的值与数据进行同步 。你可以添加lazy修饰符,从而转为在change事件(比如下面代码中的文本框的失去焦点或者按Enter键)之后进行同步,而不是立马回显。

在这里插入图片描述

.trim

如果要自动过滤用户输入的首尾空白字符,可以给v-model添加trim修饰符。

<template>
  <input v-model="cnt">
  <p>{{cnt}}</p>
  <button @click="cout">get</button>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      cnt: 0
    }
  },
  methods: {
    cout() {
      alert(this.cnt)
    }
  }
}
</script>

![在这里插入图片描述](https://img-blog.csdnimg.cn/834931190485466abde2184bbd1286b6.pn

组件基础

单文件组件

Vue单文件组件(又名*.vue文件,缩写为SFC)是一种特殊的文件格式,它允许将Vue组件的模板、逻辑与样式封装在单个文件中。

加载组件

第一步:引入组件import MyComponentVue from ' /components/MyComponent.vue'

第二步:挂载组件components: { MyComponentVue }

第三步:显示组件<my-componentVue />

<!-- HelloWorld.vue -->
<template>
  <h1>组件</h1>
</template>

<script>
  export default {
    name: "HelloWorld" <!--加上name是规范-->
  }
</script>

<!-- scoped表示样式只在当前组件生效 -->
<style scoped>
  h1 {
    color: blue
  }
</style>
<!-- App.vue -->
<template>
  <img alt="Vue logo" src="./assets/logo.png">
  <HelloWorld />
  <hello-world />
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

在这里插入图片描述

组件的组织

通常一个应用会以一棵嵌套的组件树的形式来组织

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FeatherWaves

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值