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

javascript - property this.$el undefined on single file component vue js - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a global component using laravel mix with vue js, but when accessing property this.$el it's undefined. Here's my component file:

Datepicker.vue

<template>
<input 
    type="text" 
    :name="name" 
    :class="myclass" 
    :placeholder="placeholder"
    :value="value" />
</template>  

<script>
export default {
  props: ['myclass','name','placeholder','value'],
  data () {
    return {

    }
  },
  created () {
    console.log("this.$el", this.$el); //undefined
    console.log("this", this); //$el is defined
    var vm = this;
    var options = {
        "locale": "es",        
        "onChange": function(selectedDates, dateStr, instance) {
            vm.$emit('input', dateStr);
        }
      };

    $(this.$el)
      // init
      .flatpickr(options);      
  },
  destroyed(){
    console.log("destroyed");
  }
}
</script>

But, when the component is created as X-Template it works:

client.js

Vueponent('date-picker', {
  props: ['myclass','name','placeholder','value'],
  template: '#datepicker-template',
  mounted: function () {
    var vm = this;
    var options = {
        "locale": "es",        
        "onChange": function(selectedDates, dateStr, instance) {
            vm.$emit('input', dateStr);
        }
      };  

    $(this.$el)
      // init
      .flatpickr(options);      
  },
  destroyed: function () {
    console.log("destroyed");
  }
});

create.blade.php

<script type="text/x-template" id="datepicker-template">
    <input 
    type="text" 
    :name="name" 
    :class="myclass" 
    :placeholder="placeholder" />
</script>

I'm trying to create a global component using laravel mix with vue js, but when accessing property this.$el it's undefined. Here's my component file:

Datepicker.vue

<template>
<input 
    type="text" 
    :name="name" 
    :class="myclass" 
    :placeholder="placeholder"
    :value="value" />
</template>  

<script>
export default {
  props: ['myclass','name','placeholder','value'],
  data () {
    return {

    }
  },
  created () {
    console.log("this.$el", this.$el); //undefined
    console.log("this", this); //$el is defined
    var vm = this;
    var options = {
        "locale": "es",        
        "onChange": function(selectedDates, dateStr, instance) {
            vm.$emit('input', dateStr);
        }
      };

    $(this.$el)
      // init
      .flatpickr(options);      
  },
  destroyed(){
    console.log("destroyed");
  }
}
</script>

But, when the component is created as X-Template it works:

client.js

Vue.component('date-picker', {
  props: ['myclass','name','placeholder','value'],
  template: '#datepicker-template',
  mounted: function () {
    var vm = this;
    var options = {
        "locale": "es",        
        "onChange": function(selectedDates, dateStr, instance) {
            vm.$emit('input', dateStr);
        }
      };  

    $(this.$el)
      // init
      .flatpickr(options);      
  },
  destroyed: function () {
    console.log("destroyed");
  }
});

create.blade.php

<script type="text/x-template" id="datepicker-template">
    <input 
    type="text" 
    :name="name" 
    :class="myclass" 
    :placeholder="placeholder" />
</script>
Share Improve this question edited Jul 30, 2017 at 17:56 ivanjj22 asked Jul 30, 2017 at 16:58 ivanjj22ivanjj22 1872 gold badges3 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

$el doesn't exist until mounted.

mounted() {
  var vm = this;
  var options = {
      "locale": "es",        
      "onChange": function(selectedDates, dateStr, instance) {
          vm.$emit('input', dateStr);
      }
    };

  $(this.$el)
    // init
    .flatpickr(options);      
},

See the lifecycle diagram in the documentation.

发布评论

评论列表(0)

  1. 暂无评论