Vue.js 在另一个组件中使用一个组件内的变量

Vue.js use variable within a component in another component

我有一名员工 table。每个员工都有一个角色,我正在尝试使用单选按钮(例如单选按钮管理员或超级管理员)进行过滤。

如何在另一个组件中使用一个组件内的变量?现在我有这个:

   <template id="searchemployees">
        <div class="form-group">
            <label class="col-md-2 control-label">Filter</label>

            <div class="col-md-10">
                <div class="radio radio-primary">
                    <label>
                        <input type="radio" name="intern" id="intern" value="intern" v-model="selectedrole"/>
                        Toon alles
                    </label>
                </div>
                <div class="radio radio-primary">
                    <label>
                        <input type="radio" name="employee" id="employee" value="employee" v-model="selectedrole">
                        Stagiaire
                    </label>
                </div>
            </div>    
        </div>
    </template>

 <template id="employees">
        <table class="table">
            <thead>
            <tr>
                <th>Logo</th>
                <th>Bedrijfsnaam</th>
                <th>Plaats</th>
            </tr>
            </thead>
            <tbody>
                <tr class="table-row-link" data-href="" v-for="employee in list | role selectedrole">
                    <td><img class="img-circle-company" src=""/></td>
                    <td>@{{ employee.role.RoleName }}</td>
                    <td>@{{ employee.LastName }}</td>
                </tr>
            </tbody>
        </table>
    </template>

<script>
        Vue.config.debug = true;

        Vue.filter('role',function(value,role)
        {
            if(!role || 0 === role.length)
            {
                return value;
            }
            return value.filter(function(item) {
                return item.role.RoleName == role
            });
        });

        Vue.component('searchemployees', {
            template: '#searchemployees'
        });

        Vue.component('employees', {
            template: '#employees',
            props:['list'],
            created() {
                this.list = JSON.parse(this.list);
            }
        });

        new Vue({
            el: 'body'
        });
</script>

在我的 #searchemployees 中,它选择了正确的角色。但是如何在 #employees 中将其用于我的过滤器?

谢谢

您应该能够通过分派事件将 属性 从组件向上发送到根/全局 Vue 实例。调度事件意味着您正在向 Vue 树中组件的父级发送一条包含 属性 的消息。您使用以下语法分派事件:

this.$dispatch('event-name', this.list);

这表示,将 ID 为 event-name 的事件发送到组件的父级,其中包含 属性 this.list

如果您的组件的父级正在侦听 ID 为 event-name 的确切消息,则它只能接收此分派的 属性。您可以使用以下代码(在父级中)从父级组件中监听事件:

events: {
    'event-name': function(property) {
        console.log("Property from child component" + property);
    }
}

总体而言,您的代码应如下所示:

Vue.component('employees', {
    template: '#employees',
    props:['list'],
    created() {
        this.list = JSON.parse(this.list);
        this.$dispatch('send-list', this.list);
    }
});

new Vue({
    el: 'body',
    data: {
        rootlist: {}
    },
    events: {
        'send-list': function(property) {
            this.rootlist = property;
            console.log("Property from child component" + property);
        }
    }
});

这将使您的所有 [list] 数据在您的根 Vue 实例中可用。如果您想将它向下发送到所有组件,您可以在 send-list 函数中创建一个广播事件 this.$broadcast('send-list-down', this.list);$broadcast 做与 $dispatch 完全相同的事情,但它将数据向下而不是向上发送到 Vue 树。只需确保在组件中为 send-list-down 事件创建事件侦听器即可。