vue.js - 在laravel中导入vue包

标签 vue.js vue-component laravel-5.6 vue-select

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

错误:

ncaught TypeError: Vue.component is not a function

At line:

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

到目前为止我尝试过这个:

assets/js/bootstrap.js:

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

Assets /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
        };
    },
...

关于vue.js - 在laravel中导入vue包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925886/

相关文章:

vue.js - 完整日历 Vue JS 组件 - 添加事件

vue.js - vue 计算 : add new computed value from created lifecycle hook

php - Laravel 在服务器上传后无法访问存储在存储文件夹中的图像

laravel-5.5 - 如何在 Laravel 框架中降级? (5.6 到 5.5)

javascript - Vue.js - 如何获取和设置组件输入字段的值?

node.js - vue-cli 加载程序不使用 webpack-simple 的默认初始化

unit-testing - 用 jest 测试 Vue 过滤器

javascript - vue-qrcode-component 无法保留引号

node.js - 使用vuejs和nodejs从mongodb数据库读取数据

php - laravel Eloquent 地从同一列的两个字段中获取最小值