代码之家  ›  专栏  ›  技术社区  ›  enbermudas

子组件未在方法对象的函数内部发出

  •  1
  • enbermudas  · 技术社区  · 7 年前

    我的父组件有下一段代码:

    <v-dialog max-width="400" v-model="loginDialog" @login-success="loginDialog = false"><login-component></login-component></v-dialog>
    

    我的孩子组件有一个:

    methods: {
        async handleLoginForm() {
          try {
            const response = await this.$http.post('/api/users/login', { user: this.user })
            this.$store.dispatch('authenticate', response.data.user)
            this.$emit('login-success')
          } catch(error) {
            this.$swal.mixin({
              toast: true,
              position: 'top-end',
              showConfirmButton: false,
              timer: 3000
            }).fire({
              type: 'error',
              title: error.message
            })
          }
        }
      }
    

    一切工作都在我的 handleLoginForm() 方法,除了一件事: this.$emit('login-success') 什么都不做。我的代码有什么问题吗?

    编辑: 我刚修改了我的代码,Kamel案例建议不起作用。

    0 回复  |  直到 7 年前
        1
  •  1
  •   Nicolas Durant    7 年前

    正如菲尔在他的评论中提到的,您的问题是您在对话框中添加了@login success=“login dialog=false”,而不是在发出消息的登录组件上。

        2
  •  0
  •   enbermudas    7 年前

    嗯,我找到了解决这个错误的方法。首先,我使用 .sync 使用prop实现双向数据绑定的修饰符:

    <v-dialog max-width="400" v-model="loginDialog"><login-component :dialog.sync="loginDialog"></login-component></v-dialog>
    

    我只需要发出新的值:

    this.$emit('update:dialog', false)
    
    推荐文章