javascript - 如何使用 vue.js 从 Promise 内的方法更新数据值?

标签 javascript vue.js promise

我的组件脚本是:

export default {
  name: "Authenticate",
  data: () => {
    return {
      validationFailed: {}
    };
  },
  methods: {
    validateForm() {
      this.validationFailed = {};
      if (this.createEmail.trim().length === 0) {
        this.validationFailed.createEmailField = "Email cannot be blank. ";
      }

      if (this.createPassword.trim().length === 0) {
        this.validationFailed.createPasswordField =
          "Password cannot be blank. ";
      }

      if (Object.keys(this.validationFailed).length === 0) {
        return true;
      }

      return false;
    },
    handleSubmit() {
      const that = this;
      axios
        .request({
          url: `${process.env.VUE_APP_API_URL}/users`,
          method: "POST",
          data: {
            email: this.createEmail,
            password: this.createPassword
          }
        })
        .then(response => {
          console.log(response);
        })
        .catch(err => {
          that.validationFailed.createEmailField = "something";
        });
    }
  }
};

但是在 catch 中,使用调试器,我可以看到该值已设置。但在我的 template 中,validationFailed 没有更新。我做错了什么?

最佳答案

这是 Vue react 性问题。您需要将 this.validationFailed 分配给新对象。您可以在catch block 中尝试ES6语法:

that.validationFailed = {
    ...that.validationFailed,
    createEmailField: 'something'
}

关于javascript - 如何使用 vue.js 从 Promise 内的方法更新数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56971583/

相关文章:

grails - Groovy PromiseMap - 我可以限制异步线程池吗?

typescript - 如何声明 Rx.Observable.fromPromise() 的 TypeScript 返回类型

javascript - 在我的页面上包含 jquery 库有什么缺点吗?

Javascript 验证具有最小文本长度的字母数字

Vue.js 问题 : Prop doesn't change image v-bind:src

javascript - 在JSP中使用Vue.js,标签/指令冲突

javascript - jQuery .when 未按预期与休息运算符一起工作

javascript - 100 多张图片的图片 src - iOS 应用程序大小问题

javascript - MongoDB/Mongoose/nodejs 中的引用资料 - 并行化

javascript - Node.js 应用程序在本地正常工作,但在 Droplet Ubuntu 上出现 404 错误