最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue - how do I watch an input with an existing value - Stack Overflow

programmeradmin0浏览0评论

I have an email input and a watcher that checks if the input is valid or not. However, the watch event only gets triggered when you type something on the email field. The email field has an existing value that may or may not be invalid. An x is displayed if the email is invalid and a check otherwise. However, since the email has an existing value, the check is initially displayed even when the input is invalid. If the user clears the email field, and retypes the invalid email, that's the only time the watcher will be triggered and the x will be displayed.

Here's my vue instance:

var register = new Vue({
    el: '#register',
    data: {
        email: document.getElementById('profileEmail').value,
        isEmailTaken: false,
        emailTimer: null,
        emailBusy: false
    },
    methods: {
        validateEmail: function(email) {
            var self = this;
            var url  '/api/users?email=' + email;
            self.$http.get(url)
                .then(function(res){
                    self.isEmailTaken = true;
                    self.emailBusy = false;
                }, function(err){
                    self.isEmailTaken = false;
                    self.emailbusy = false;
                });
    },
    watch: {
        email: function(val) {
            var self = this;
            clearTimeout(self.emailTimer);
            self.emailBusy = true;
            self.emailTimer = setTimeout(function() {
                self.validateEmail(val);
            }, 1600);
        }
    }
}

This is my email input (in pug):

.form-group
  label Email Address
  .input-group
    input.form-control(type="email" name="emailAddress" value=profile.email
      v-model="email"
      v-validate
      data-vv-delay="1000"
      data-vv-rules="required|email"
      data-vv-as="email"
      :class="{ 'input': true, 'is-danger': errors.has('emailAddress') }"
      placeholder="eg. [email protected]")
    .input-group-append
      span.input-group-text
        i.fal.fa-pulse.fa-spinner(v-if="email && emailBusy")
        i.fal.fa-check.text-green(v-if="email && !emailBusy && !isEmailTaken && !errors.has('emailAddress')")
        i.fal.fa-times.text-red(v-if="email && !emailBusy && (isEmailTaken || errors.has('emailAddress'))")

I have an email input and a watcher that checks if the input is valid or not. However, the watch event only gets triggered when you type something on the email field. The email field has an existing value that may or may not be invalid. An x is displayed if the email is invalid and a check otherwise. However, since the email has an existing value, the check is initially displayed even when the input is invalid. If the user clears the email field, and retypes the invalid email, that's the only time the watcher will be triggered and the x will be displayed.

Here's my vue instance:

var register = new Vue({
    el: '#register',
    data: {
        email: document.getElementById('profileEmail').value,
        isEmailTaken: false,
        emailTimer: null,
        emailBusy: false
    },
    methods: {
        validateEmail: function(email) {
            var self = this;
            var url  '/api/users?email=' + email;
            self.$http.get(url)
                .then(function(res){
                    self.isEmailTaken = true;
                    self.emailBusy = false;
                }, function(err){
                    self.isEmailTaken = false;
                    self.emailbusy = false;
                });
    },
    watch: {
        email: function(val) {
            var self = this;
            clearTimeout(self.emailTimer);
            self.emailBusy = true;
            self.emailTimer = setTimeout(function() {
                self.validateEmail(val);
            }, 1600);
        }
    }
}

This is my email input (in pug):

.form-group
  label Email Address
  .input-group
    input.form-control(type="email" name="emailAddress" value=profile.email
      v-model="email"
      v-validate
      data-vv-delay="1000"
      data-vv-rules="required|email"
      data-vv-as="email"
      :class="{ 'input': true, 'is-danger': errors.has('emailAddress') }"
      placeholder="eg. [email protected]")
    .input-group-append
      span.input-group-text
        i.fal.fa-pulse.fa-spinner(v-if="email && emailBusy")
        i.fal.fa-check.text-green(v-if="email && !emailBusy && !isEmailTaken && !errors.has('emailAddress')")
        i.fal.fa-times.text-red(v-if="email && !emailBusy && (isEmailTaken || errors.has('emailAddress'))")
Share Improve this question edited Apr 5, 2018 at 8:15 haMzox 2,1091 gold badge15 silver badges25 bronze badges asked Apr 5, 2018 at 7:56 Bargain23Bargain23 1,9734 gold badges32 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Vue has an option for this.

immediate: true. Meaning that the watch function will be executed with the initial value in the beginning as well. See Option: immediate in the api doc.

export default {
  //...
  watch: {
    email: {
      handler: function(val) {
        /* ... */
      },
      immediate: true
    }
  }
  //...
};
发布评论

评论列表(0)

  1. 暂无评论