簡體   English   中英

VueJS-初始化作為模板或$ el屬性的一部分加載的tagsinput表單字段?

[英]VueJS - Initializing a tagsinput form field that was loaded as part of template or $el attribute?

我正在遵循使用組件加載視圖的官方文檔中描述的模式。 其中的一個組件具有一個form字段,由於我使用的是TagInput,因此我需要調用一個名為.tagsinput()的方法。 因此,類似$('#tags').tagsinput() 這是我正在做的簡化版本:

  CreateBoardForm = Vue.extend
    template: "<input type='text' v-text='tags' id='tags'/>"
    data:
      tags: ''
    ready: ->
      // this is where I'm hoping to access
      // tags and call $('#tags').tagsinput() on it
      // However, this.$el and this.template are all undefined
      // I was hoping to do something like this.$el.find('#tags').tagsinput()

  Vue.component('CreateBoardForm', CreateBoardForm)

  vue = new Vue(
    el: '#main',
    data:
      currentView: 'createBoardForm'
    components:
      createBoardForm: CreateBoardForm
  )

我將如何初始化該表單字段的任何幫助將不勝感激。

謝謝

好,我知道了。 基本上,您必須創建一個新組件,偵聽附加事件,使用計算的屬性,然后使用v-ref標記,該標記將成為對標記輸入的引用。 我從這個標簽輸入庫切換到另一個,但是想法是一樣的。 這是一個工作的JSFiddle ,下面是代碼:

<div id="tags-input-example">
    <tags-input v-ref="twitterUsers"></tags-input>
    <input type="button" v-on="click: onSubmit" value="Submit"/>        
</div>

<script type="text/x-template" id="tags-input">
    <input type="text" />
</script>

Vue.component('tags-input', {
    template: "#tags-input",
    attached: function() {
        $(this.$el).find('input').tagsInput();
    },
    computed: {
        tags: {
            get: function () {
                return $(this.$el).find('input').val();
            }
        }    
    }
});

vm = new Vue({
    el: '#tags-input-example',
    methods: {
        onSubmit: function(e) {
            console.log(this.$.twitterUsers.tags);
            alert("The tags are: " + this.$.twitterUsers.tags);
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM