在laravel中导入vue包

2023-12-02

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

Error:


ncaught TypeError: Vue.component is not a function  

At line:

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"
    }

我认为简单的就可以了

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

然后在你的组件中你可以导入你需要的东西:

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

export default {
    data() {
        return {
            keywords: null,
            options: [],
            item: null
        };
    },
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在laravel中导入vue包 的相关文章

随机推荐