【Vue】事件绑定和计算属性

Event-binding

html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Basics</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Jost:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

    <script src="app.js" defer></script>
  </head>

  <body>
    <header>
      <h1>Vue Events</h1>
    </header>
    <section id="events">
      <h2>Events in Action</h2>
      <button @click="addCounter(10)">Add 10</button>
      <button @click.right="reduceCounter(5)">Reduce 5</button>
      <p v-once>Starting Counter: {{ counter }}</p>
      <p>Result: {{ counter }}</p>
      <input
        type="text"
        @:input="setName($event, 'Qiu')"
        @:keyup.enter="confirmInput"
      />
      <p>Your Name: {{ confirmedName }}</p>
      <hr />
      <form @:submit.prevent="submitForm">
        <input type="text" />
        <button>Sign Up</button>
      </form>
    </section>
  </body>
</html>

JS

const app = Vue.createApp({
  data() {
    return {
      counter: 10,
      userName: "",
      confirmedName: "",
    }
  },
  methods: {
    addCounter(num) {
      this.counter += num
    },
    reduceCounter(num) {
      this.counter >= 1 ? (this.counter -= num) : 0
    },
    setName(event, lastName) {
      this.userName = `${event.target.value}${lastName}`
    },
    submitForm() {
      // event.preventDefault();
      alert("Submitted!")
    },
    confirmInput() {
      this.confirmedName = this.userName
    },
  },
})

app.mount("#events")

效果

展示效果
two way binding:v-model==v-bind:value v-on:input

计算属性

之前学了data binding和event binding。
methods并不是有关动态值计算的最佳方法,每当页面上的任何内容被改动时,因为vue并不知道methods是否需要渲染,也许methods包含改动的内容,所以任何非事件绑定方法都会被vue重新执行,因此引入第三个功能:计算属性(computed properties)

计算属性原理类似于methods,但是vue能够知道他们的依赖关系

示例

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Basics</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Jost:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <script src="https://unpkg.com/vue@3/dist/vue.global.js" defer></script>
    <script src="app.js" defer></script>
  </head>

  <body>
    <header>
      <h1>Vue Events</h1>
    </header>
    <section id="events">
      <h2>Events in Action</h2>
      <button v-on:click="add(10)">Add 10</button>
      <button v-on:click="reduce(5)">Subtract 5</button>
      <p>Result: {{ counter }}</p>
      <input type="text" v-model="name" />
      <button @click="ResetInput">Reset Input</button>
      <p>Your Name: {{ fullname }}</p>
    </section>
  </body>
</html>
const app = Vue.createApp({
  data() {
    return {
      counter: 0,
      name: "",
      fullname: "",
    }
  },
  watch: {
    counter(value) {
      if (value > 50) {
        this.counter = 0
      }
    },
    name(value) {
      if (value === "") {
        return (this.fullname = "")
      } else {
        this.fullname = value + " " + "Qiu"
      }
    },
  },
  computed: {
    fullnameFunction() {
      console.log("Running again...")
      if (this.name === "") return ""
      return this.name + " " + "Jerry"
    },
  },
  methods: {
    setName(event, lastName) {
      this.name = event.target.value + " " + lastName
    },
    add(num) {
      this.counter = this.counter + num
    },
    reduce(num) {
      this.counter = this.counter - num
      // this.counter--;
    },
    resetInput() {
      this.name = ""
    },
  },
})

app.mount("#events")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jerry_ww

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

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

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

打赏作者

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

抵扣说明:

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

余额充值