【问题标题】:how can a Buefy toast be made available to all components in a Vue app?如何让 Vue 应用程序中的所有组件都可以使用 Buefy Toast?
【发布时间】:2021-02-13 23:24:15
【问题描述】:

在 Vue 应用程序的组件中,以下方法在用户单击表单上的提交按钮后运行:

execute() {                    
            let message = '';
            let type = '';

            const response = this.actionMode == 'create' ? this.createResource() : this.updateResource(this.data.accountId);

            response.then(() => {
                message = 'Account ' + this.actionMode + 'd for ' + this.data.name;
                type = 'is-success';
            })
            .catch(e => {
                message = 'Account <i>NOT</i> ' + this.actionMode + 'd<br>' + e.message;
                type = 'is-danger';
            })
            .then(() => {
                this.displayOutcome(message, type);
                this.closeModal();
            });
        }

同一组件中的displayOutcome() 方法如下所示:

displayOutcome(message, type) {
            this.$buefy.toast.open({
                duration: type == 'is-danger' ? 10000 : 3500,
                position: 'is-bottom',
                message: message,
                type: type
            });
        }

代码在组件中运行良好。现在我正在尝试将 displayOutcome() 方法移动到 helpers.js 文件中并导出该函数,以便应用程序中的任何组件都可以导入它。这将集中维护 toast 并防止在需要的每个组件中写入单独的 toast。无论如何,当displayOutcome() 被移动到 helpers.js,然后导入到组件中时,触发函数时控制台中会出现错误:

我怀疑它与引用 Vue 实例有关,所以我尝试了 main.js 文件并更改了它

new Vue({
    router,
    render: h => h(App),
}).$mount('#app');

到这里

var vm = new Vue({
    router,
    render: h => h(App),
}).$mount('#app');

然后在 helpers.js 中

export function displayOutcome(message, type) {
    // this.$buefy.toast.open({
    vm.$buefy.toast.open({
        duration: type == 'is-danger' ? 10000 : 3500,
        position: 'is-bottom',
        message: message,
        type: type
    });
}

但这导致“编译失败”。错误信息。

是否有可能使 helpers.js 中的displayOutcome() 以某种方式工作?

【问题讨论】:

    标签: vue.js buefy


    【解决方案1】:

    displayOutcome() 需要对this 的引用才能工作,如果您将其定义为组件对象上的方法(标准方式),这很好。但是,当您在外部定义它时,您只需提供任何 函数 而不是 method,它是一个“针对”对象的函数。这个“定位”是通过this 完成的。因此,当您从外部文件传递一个简单函数时,没有与特定对象的关联,因此没有可用的this

    要克服这个问题,您可以使用displayOutcome.apply(thisArg, methodArgs),其中thisArg 将是您函数中的this 引用,methodArgs 是传递给函数的剩余参数。

    所以displayOutcome.apply(4, ['some', 'thing']) 意味着在这种情况下displayOutcome() 中的this 引用变为4

    进一步阅读:

    import { displayOutcome } from './component-utils'
    
    // when calling displayOutcome() from within your component
    displayOutcome.apply(this, ['Hello World', 'is-info'])
    
    // component-utils.js
    export function displayOutcome(message, type) {
        // this.$buefy.toast.open({
        this.$buefy.toast.open({
            duration: type == 'is-danger' ? 10000 : 3500,
            position: 'is-bottom',
            message: message,
            type: type
        });
    }
    

    【讨论】:

    • 太棒了!需要进行一次小的调整才能使其正常工作:displayOutcome.apply(this, ['Hello World', 'is-info'])。控制台错误消息提供了足够的指导“... Function.prototype.apply 的第二个参数必须是一个数组”。
    • 对,我总是把这两者混为一谈。感谢您指出这一点,我已经相应地更新了答案!仅供参考,除了 apply 之外还有另一种方法 - .call(),它不接受数组而是列表。 func.apply(myThis, [arg1, arg2])func.call(myThis, arg1, arg2).
    【解决方案2】:

    您可以在 Vuex 商店中创建一个动作并从任何组件中调度它。

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2021-11-08
      相关资源
      最近更新 更多