VueJS - 初始化作为模板或$el属性的一部分加载的标签输入表单字段



我遵循使用组件加载视图的官方文档中描述的模式。 其中一个组件有一个表单字段,我需要调用一个名为.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
  )

关于如何初始化该表单字段的任何帮助将不胜感激。

谢谢

好的,我想通了。 基本上,您必须创建一个新组件,侦听附加的事件,使用计算属性,然后使用 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);
        }
    }
});

最新更新