Vuejs2 元素UI-提示和输入验证

Vuejs2 元素UI-提示和输入验证,vuejs2,Vuejs2,我正在使用最新版本的vue js和element ui 我正在尝试使用inputValidator函数来验证输入 上面说我们可以使用函数,但我不能让它工作 var Main = { methods: { validateInput (input) { if (input && input.length > 5) { return 'input has to be 5 length' } else ret

我正在使用最新版本的
vue js
element ui

我正在尝试使用
inputValidator
函数来验证输入

上面说我们可以使用函数,但我不能让它工作

var Main = {
    methods: {
      validateInput (input) {
        if (input && input.length > 5) {
          return 'input has to be 5 length'
        } else return true;
      },
      open3() {
        this.$prompt('Input (limit 5)', 'Tip', {
          confirmButtonText: 'OK',
          cancelButtonText: 'Cancel',
          inputValidator: this.validateInput()
        }).then(value => {
        console.log(value);
          this.$message({
            type: 'success',
            message: 'Your input is:' + value
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: 'Input canceled'
          });       
        });
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
正如@thanksd所说:


inputValidator:this.validateInput
而不是
inputValidator:this.validateInput()
您正在调用
this.validateInput()
并将结果作为
inputValidator
选项传递

您需要传递对该方法的引用,因此只需传递
this.validateInput

this.$prompt('Input (limit 5)', 'Tip', {
  confirmButtonText: 'OK',
  cancelButtonText: 'Cancel',
  inputValidator: this.validateInput
})