javascript - Event_Bus vue 变量访问单文件组件 Vue.js - Laravel Mix

标签 javascript laravel vue.js vuejs2 laravel-mix

我只是想问一下vue的单文件组件如何访问变量? 当我尝试在浏览器上访问我的应用程序时,vue js 将导致错误。

Error in mounted hook: "ReferenceError: EventBus is not defined"

下面是我的代码。

我在 app.js 上实现了这个

window.Vue = require('vue');
var EventBus = new Vue();

    Vue.component('transactionslist', require('./components/TransactionList.vue'));

    const app = new Vue({
        el: '#transaction',
        methods: {
            getTransactions: function () {
                $.getJSON("/guard/station/transactions?search=" + this.Search, function (_transactions) {
                    this.Transactions = JSON.parse(JSON.stringify(_transactions));
                }.bind(this));
            }
        },
        data() {
            return {
                Search: '',
                Transactions: [],
            }
        },
        mounted: function () {
            this.getTransactions();

        }
    });

这是我的 vue 组件 (TransactionList.vue)

<template>
    <table class="table table-hover transaction-list">
        <thead>
            <tr>
                <th class="fit">Transfer Code</th>
                <th>Orgin</th>
                <th>Destination</th>
                <th class="fit">Date Created</th>
            </tr>
        </thead>
        <tbody>
            <transactionRow @rowSelected="isSelectedRow" v-for="trans in data" :origin="trans.Origin" :destination="trans.Destination"
                :date="trans.created_at" :key="trans.TransferCode" :tscode="trans.TransferCode">
            </transactionRow>
        </tbody>
    </table>
</template>

<script>
    export default {
        props: ['data'],
        created() {
            this.transactions = this.$children;
        },
        data() {
            return {
                transactions: [],
                Transaction: []
            }
        },
        methods: {
            isSelectedRow: function (payload) {
                EventBus.$emit('showSelected', payload);
                this.transactions.forEach(transaction => {
                    transaction.isActive = (transaction.tscode == payload.tscode);
                });
            },
        }

    }
</script>

我使用 Laravel 开发应用程序,并使用 Laravel 功能之一,即 Laravel Mix。

最佳答案

只需使用这个:

window.EventBus = new Vue();

关于javascript - Event_Bus vue 变量访问单文件组件 Vue.js - Laravel Mix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49295236/

相关文章:

javascript - 有没有办法执行一个函数或将一个动态类分配给一对两个组件,从数组的迭代中跳过每两个组件?

javascript - 创建具有自动调整大小的文本区域

javascript - Angular 2 数据表

php - 禁用 Laravel 内置的错误处理方法

Laravel Dusk 无法断言看到屏幕截图中可见的文本(使用 whenAvailable 作为模态)

javascript - 如何根据第二个响应用axios更新第一个响应的内容

javascript - 如何使 router-link 与自定义链接组件一起工作?

javascript - Promise Pending 不会解决或拒绝

javascript - JavaScript 控制台中的值与数组不同

laravel - ResourceCollection 不包括分页链接