vue.js 提交表单时应发出submit事件

7eumitmz  于 2023-05-01  发布在  Vue.js
关注(0)|答案(1)|浏览(220)

我有一个vue login in组件,它没有通过测试:

LoginForm should emit submit event

Error:
  submit event should be emitted when submitting form

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

按钮:

<button type="button" id="login-button" :disabled="!enableSubmit" @click="submit()">Submit</button>

方法:

submit() {
    if(this.username != "" && this.password != "") {
      this.$emit("You have successfully loged in", this.username && this.password)
    } else {
      console.log("Not logged in")
    }
  }

如何正确地发出submit事件?

ztyzrc3y

ztyzrc3y1#

submit() {
  if(this.username != "" && this.password != "") {
    this.$emit("submit", this.username && this.password)
  } else {
    console.log("Not logged in")
  }
}

在父组件中,可以监听子组件发出的submit事件,如下所示:

<LoginForm @submit="handleSubmit" />

methods: {
  handleSubmit(data) {
    console.log(data)
  }
}

相关问题