【问题标题】:Getting asynchronous values from ant-design vue form using onValuesChange使用 onValuesChange 从 ant-design vue 表单获取异步值
【发布时间】:2021-09-18 00:22:13
【问题描述】:

我有一个使用 ant-design-vue 提交数据并将数据发送到后端的表单。但是,我想要实现的是给用户某种形式的反馈,这样当他们在字段中输入时,他们会看到值 {fullname placeholder} 立即更新,然后单击提交按钮将其发送到后端。

{{ fullname || 'Your Name' }}

 <a-col :xs="{ span: 24 }" :lg="{ span: 12 }">
              <a-form-item label="Full Name">
                <a-input
                  type="text"
                  placeholder="Your Name"
                  :disabled="toggleEdit === 'edit'"
                  v-decorator="[
                    'fullname',
                    {
                      initialValue: this.fullname || '',
                      rules: [{ required: true, message: 'Name is required!' }],
                    },
                  ]"
                  autocomplete="name"
                /> </a-form-item
            ></a-col>

所以顶部的 {{ fullname }} 会立即更新用户类型,类似于 v-model。但我想知道如何使用 onValuesChange 方法以 ant-design-vue 形式实现这一点。

【问题讨论】:

    标签: forms vue.js antd


    【解决方案1】:

    您需要首先使用v-model 将您的值绑定到input。 同时,使用submit方法在按钮上使用@click

    <a-input
      type="text"
      v-model="inputValue"  <---here
      placeholder="Your Name"
      :disabled="toggleEdit === 'edit'"
      v-decorator="['fullname',
      {
         initialValue: this.fullname || '',
         rules: [{ required: true, message: 'Name is required!' }],
       },
      ]"
      autocomplete="name"/>
    <button @click="submit"></button>
    ...
    
    data(){
      return{
         inputValue: ""
      }
    }
    
    methods:{
      submit(){
       // I send keyword with a object which include this inputValue by POST method
       // you can change it to yours, and keyword as well
       fetch("api-url").post({ keyword: this.inputValue }) 
       .then(response.json())
       .then(res => { //dosomthing when you get res from back-end })
    }
    }
    

    上面的代码是你的要求吗?

    【讨论】:

    • 实际上,我可以很容易地插入 v-model 但我不能,因为我使用的是 antdv 库。它不适用于 v-decorator,总是抛出错误。但是,我通过添加一个@change 监听器然后监听事件并用它做任何我想做的事情来解决它。
    猜你喜欢
    • 2019-04-26
    • 2019-10-28
    • 1970-01-01
    • 2019-05-11
    • 2020-10-25
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多