Vue选择插槽范围未定义

时间:2018-07-31 16:57:41

标签: javascript vue.js vuejs2 vue-component vue-select

我已经调试了8个小时,试图找出为什么这个vue-select代码不起作用,但是仍然没有运气。当我运行它时,它显示错误“ 选项未定义”。我正在使用npm将vue-select插件安装到我的项目中。我将以下html代码放入我的树枝模板中。我确实按照vue-select page

上的说明进行操作
<div id="myapp">
        <v-select :options="myoptions" label="title">
            <template slot="option" slot-scope="option">
                <span class="fa" :class="option.icon"></span>
                    {{ option.title }}
            </template>
        </v-select>
</div>

这是我的其余js实现代码。

import Vue from 'vue';
import VueSelect from 'vue-select';        
Vue.component('v-select', VueSelect)

new Vue({
    el: '#myapp',
    data: function () {
        return {
            myoptions: [
                {
                    title: 'Read the Docs',
                    icon: 'fa-book',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                },
                {
                    title: 'View on GitHub',
                    icon: 'fa-github',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                },
                {
                    title: 'View on NPM',
                    icon: 'fa-database',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                },
                {
                    title: 'View Codepen Examples',
                    icon: 'fa-pencil',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                }
            ]
        }
    }
});

2 个答案:

答案 0 :(得分:1)

这是我的测试组件,可以正常运行。 我的Vue依赖关系如下

  "dependencies": {
    "vue": "^2.5.16",
    "vue-select": "^2.4.0"
  },

App.vue组件

<template>
  <div id="app">
    <v-select :options="options" label="title">
            <template slot="option" slot-scope="option">
                <span class="fa" :class="option.icon"></span>
                    {{ option.title }}
            </template>
    </v-select>
  </div>
</template>

<script>
export default {
  name: 'app',
  data: function() {
        return {
            options: [
                {
                    title: 'Read the Docs',
                    icon: 'fa-book',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                },
                {
                    title: 'View on GitHub',
                    icon: 'fa-github',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                },
                {
                    title: 'View on NPM',
                    icon: 'fa-database',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                },
                {
                    title: 'View Codepen Examples',
                    icon: 'fa-pencil',
                    url: 'https://codeclimate.com/github/sagalbot/vue-select'
                }
            ]
        }
      }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

答案 1 :(得分:0)

谢谢大家的帮助。我发现了问题,这是我的代码实现不正确。我没有将vue组件正确导入到模板中。现在工作正常。