【问题标题】:Event handling change in Vue 3.0?Vue 3.0 中的事件处理变化?
【发布时间】:2020-08-10 13:20:37
【问题描述】:

我需要处理子组件中的事件;检查某个条件;如果为真,则将“提交”事件返回给父级,以便它的事件处理程序将运行。

下面的示例使用 Vue.js 2.6.11 触发父级的事件处理程序(将“vue”替换为“@vue/composition-api”)。在 3.0.0-rc.5 中,它会触发 两次。想知道这是故意更改、错误还是我。

App.vue:

<template lang="pug">
#app
  .container
    h1 Form Experiment
    FormGroup(@submit='onSubmit')
      button.btn.btn-primary(type='submit') Submit
</template>

<script lang="ts">
import { defineComponent } from "vue"
import FormGroup from "@/components/FormGroup.vue"

export default defineComponent({
  name: "App",
  components: {
    FormGroup,
  },
  setup() {
    const onSubmit = (): void => {
      alert("Parent event handler")
    }
    return { onSubmit }
  }
})
</script>

FormGroup.vue:

<template lang="pug">
form(@submit.prevent='onFormSubmit', novalidate, autocomplete='on')
  slot Content needed in FormGroup
</template>

<script lang="ts">
import { defineComponent } from "vue"

export default defineComponent({
  name: "FormGroup",
  setup(_, { emit }) {
    const check = ref(true) // evaluated elsewhere - simplified for this example
    const onFormSubmit = (): void => {
      if (check.value) {
        alert("Form is valid - sending event to parent")
        emit("submit")
      } else {
        alert("Form is not valid.") // so don't emit the event to parent
      }
    }

    return { check, onFormSubmit }
  }
})
</script>

关于为什么在 Vue.js 3.0.0-rc.5 中父级中的 onSubmit() 触发两次的任何想法?

【问题讨论】:

  • 可能是由&lt;form&gt; 作为根元素引起的错误?如果 FormGroup 被植根为 &lt;div&gt;&lt;form...,我会很好奇是否会发生同样的事情
  • @nstuyvesant 我刚刚在 Vue 2 中使用@vue/composition-api 尝试了您的代码,它也触发了两次,所以我认为这不是 Vue 3 中的错误。Daniel 的回答应该提供解决方案。

标签: typescript vue.js event-handling vuejs3 vue-composition-api


【解决方案1】:

看起来它是在 Vue 3 中设计的

继承属性

类型:布尔值

默认:真

详情:

默认情况下,不被识别为的父范围属性绑定 道具将“失败”。这意味着当我们有一个单根时 组件,这些绑定将应用于 子组件作为普通的 HTML 属性。创作组件时 包装目标元素或另一个组件,这可能并不总是 成为期望的行为。通过将 inheritAttrs 设置为 false,这 可以禁用默认行为。属性可通过 $attrs 实例属性,并且可以显式绑定到非根 元素使用 v-bind。

注意:此选项不影响类和样式绑定。

文档:https://v3.vuejs.org/api/options-misc.html#inheritattrs

应该可以工作(添加inheritAttrs: false):

<template lang="pug">
form(@submit.prevent='onFormSubmit', novalidate, autocomplete='on')
  slot Content needed in FormGroup
</template>

<script lang="ts">
import { defineComponent } from "vue"

export default defineComponent({
  name: "FormGroup",
  inheritAttrs: false,
  setup(_, { emit }) {
    const check = ref(true) // evaluated elsewhere - simplified for this example
    const onFormSubmit = (): void => {
      if (check.value) {
        alert("Form is valid - sending event to parent")
        emit("submit")
      } else {
        alert("Form is not valid.") // so don't emit the event to parent
      }
    }

    return { check, onFormSubmit }
  }
})
</script>

const {
  defineComponent,
  createApp,
  ref,
} = Vue

const FormGroupFactory = (name, inheritAttrs) => defineComponent({
  name,
  inheritAttrs,
  setup(_, {
    emit
  }) {
    const onFormSubmit = () => {
      emit("evt", "@EVT")
      emit("submit", "@SUBMIT")
    }
    return {
      onFormSubmit
    }
  },
  template: document.getElementById("FormGroup").innerHTML
})


createApp({
    el: '#app',
    components: {
      FormGroupOne: FormGroupFactory('FormGroupOne', true),
      FormGroupTwo: FormGroupFactory('FormGroupTwo', false),
    },
    setup() {
      const log = ref('');
      const onSubmit = (e) => {
        log.value = log.value + "Parent event handler" + e + "\n"
      }
      return {
        log,
        onSubmit
      }

    }
  })
  .mount('#app')
<script src="https://unpkg.com/vue@3.0.0-rc.5/dist/vue.global.js"></script>

<div id="app">
  <div class="container">
    <form-group-one @submit="onSubmit"><button class="btn btn-primary" type="submit">Submit</button> inheritAttrs: true</form-group-one>
    <form-group-two @submit="onSubmit"><button class="btn btn-primary" type="submit">Submit</button> inheritAttrs: false</form-group-two>
  </div>
  <pre>{{log}}</pre>
</div>

<template id="FormGroup">
  <form @submit.prevent="onFormSubmit" novalidate="novalidate" autocomplete="on">
    <slot>Content needed in FormGroup</slot>
  </form>
</template>

【讨论】:

  • 但是inheritAttrs 已经是exists in Vue 2(截至2.4.0),它的文档在Vue 3 中是相同的。我认为inheritAttrs 只是一种解决方法。
  • 我没有检查 vue 2,所以我无法谈论 vue 2 中的行为,但正如您在示例中看到的那样,将其设置为 false 确实减少了一次调用跨度>
  • 那么inheritAttrs: true(这是默认值)在 vue2.6 上不起作用吗? AFAIK 似乎 vue 3 的行为正确
  • 事实证明,在 Vue 2 (plain) 和 Vue 2 w/@vue/composition-api 中的行为实际上是相同的,所以我认为这不再是一个错误,解决方案确实是正如你所说(使用inheritAttrs: false)。
  • 这很好用。奇怪的是,我在 Vue 2.6.11 和 Composition API 上没有遇到同样的问题,但你的指导是正确的。谢谢!
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2018-09-27
  • 2017-07-09
  • 2018-12-06
  • 1970-01-01
  • 2019-08-08
相关资源
最近更新 更多