刀片中的Vue组件

时间:2017-04-04 22:00:57

标签: laravel vue.js

我在我的刀片视图中尝试使用this。我有.vue文件和这个代码在js。

import Multiselect from 'vue-multiselect'

export default {
  components: {
    Multiselect
  },
  data () {
    return {
      value: '',
      options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
    }
  }
}

当我在刀片中添加组件时,就像这样`

<div>
      <label class="typo__label">Single select</label>
      <multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
      <pre class="language-json"><code>@{{value}}</code></pre>
    </div>

选择不起作用,只显示{{value}}字符串。 ¿有关错误的想法吗? `

1 个答案:

答案 0 :(得分:1)

您还需要将父组件添加到HTML中,因此如果您有主app.js,它应该是这样的。

// mycomponent.js

import Multiselect from 'vue-multiselect'

export default {
  components: {
    Multiselect
  },
  data () {
    return {
      value: '',
      options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
    }
  }
}

// app.js

var MyComponent = require('./mycomponent');

var app = new Vue({
  el: '#app',
  components: {
    MyComponent
  }
});

// index.blade.php

    <div id="app">
      <my-component inline-template>
        <div>
          <label class="typo__label">Single select</label>
             <multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
           <pre class="language-json"><code>@{{value}}</code></pre>
        </div>
      </my-component>
    </div>

因此,html中的“my-component”上下文知道并跟踪这里的值是一个小提琴,所以你可以看到它的实际效果。

const Multiselect = VueMultiselect.Multiselect;

var MyComponent = {
  components: {
    Multiselect
  },
  data() {
	  return {
      value: '',
      options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
    }
  }
};
    
var app = new Vue({
  el: '#app',
  components: {
    MyComponent
  }
});
<link href="https://unpkg.com/vue-multiselect@2.0.0-beta.14/dist/vue-multiselect.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/vue-multiselect@2.0.0-beta.14"></script>
<script src="https://vuejs.org/js/vue.min.js"></script>

<div id="app">
  <my-component inline-template>
    <div>
      <label class="typo__label">Single select</label>
      <multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
      <pre class="language-json"><code>{{value}}</code></pre>
    </div>
  </my-component>
</div>