1

コンポーネントを使用してビューをロードする公式ドキュメントに記載されているパターンに従っています。コンポーネントの 1 つにフォーム フィールドがあり.tagsinput()、私は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
  )

そのフォームフィールドを初期化する方法についての助けをいただければ幸いです。

ありがとうございました

4

1 に答える 1

1

OK、私はこれを理解しました。基本的に、新しいコンポーネントを作成し、添付されたイベントをリッスンし、計算されたプロパティを使用して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);
        }
    }
});
于 2015-01-04T21:31:59.647 に答える