2

在 laravel 5.6 中导入 vue 包的正确方法是什么?它带有 vue 和 bootstrap 预安装。我看到它们都是在公共目录的 app.js 中编译的,但我可以弄清楚如何导入https://github.com/moreta/vue-search-select并使用它。在我尝试自行导入后:

错误:

ncaught TypeError: Vue.component 不是函数

在线:

Vue.component('search-user', __webpack_require__(42));

直到现在我试过这个:

资产/js/bootstrap.js:

import { BasicSelect } from 'vue-search-select';
window.BasicSelect = BasicSelect;

资产/js/app.js:

require('./bootstrap');

window.Vue = require('vue');
window.Vue = require('vue-search-select');

Vue.component('search-user', require('./components/SearchUser.vue'));

var app = new Vue({
   el: '#app'
})

组件

<template>
    <basic-select :options="options"
                  :selected-option="item"
                  placeholder="select item"
                  @select="onSelect">
    </basic-select>
</template>

<script>

    export default {
        data() {
            return {
                keywords: null,
                options: []
            };
        },

        watch: {
            keywords(after, before) {
                if (this.keywords.length > 0)
                    this.fetch();
            }
        },

        methods: {
            fetch() {
                axios.get('/api/search', {params: {keywords: this.keywords}})
                    .then(response => this.options = response.data)
                    .catch(error => {
                    });
            },
            onSelect (item) {
                this.item = item
            },
            reset () {
                this.item = {}
            },
            selectOption () {
                // select option from parent component
                this.item = this.options[0]
            },
            components: {
                BasicSelect
            }
        }
    }

</script>

我跑了:npm install 和 npm run watch:

"devDependencies": {
        "ajv": "^6.0.0",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "laravel-mix": "^2.0",
        "lodash": "^4.17.4",
        "popper.js": "^1.12",
        "uikit": "^3.0.0-beta.35",
        "vue": "^2.5.7",
        "vue-search-select": "^2.5.0"
    },
    "dependencies": {
        "axios": "^0.17.1",
        "jquery": "^3.3.1"
    }
4

2 回答 2

2

我认为简单的就行

window.Vue = require('vue');
require('vue-search-select');

然后在您的组件中,您可以在顶部导入您需要的内容:

import { BasicSelect } from 'vue-search-select';

export default {
    data() {
        return {
            keywords: null,
            options: [],
            item: null
        };
    },
...
于 2018-02-22T11:12:05.190 回答
0

一个缺少的细节欺骗了我,您需要像这样注册组件,否则将找不到:

components: {
  ModelSelect,
  BasicSelect
},
于 2019-06-18T10:35:29.733 回答