简体   繁体   English

Laravel 和 Vue 在 Blade 中将数据从组件 1 发送到组件 2

[英]Laravel and Vue emit data from component 1 to component 2 within Blade

No idea if this is possible.不知道这是否可能。

But here is the context.但这是上下文。

I have a left navbar with a membership status(active/inactive).我有一个带有会员状态(活动/非活动)的左侧导航栏。 When the payment gateway receives the payment, a webhook is sent via Paddle(Laravel Paddle API).当支付网关收到付款时,会通过 Paddle(Laravel Paddle API)发送一个 webhook。

During the webhook I am redirecting the user to a thank you page(blade with vue component).在 webhook 期间,我将用户重定向到感谢页面(带有 vue 组件的刀片)。

app.js应用程序.js

let Event = new Vue();
window.Event = Event;

navbar.blade.php导航栏.blade.php

<componentOne></componentOne>
<componentTwo></componentTwo>

ComponentOne组件一

    mounted(){
        Event.$emit("updateStatus");
    },

ComponentTwo:组件二:

    mounted() {
        this.fetchStatus();
        Event.$on("updateStatus", () => {
            setTimeout(() => {
                console.log('testing!');
            }, 4000);
        })
    },

I was hoping to catch this even in component 2 but nothing.....我希望即使在组件 2 中也能捕捉到这一点,但什么也没有......

Any idea what I am doing wrong?知道我做错了什么吗? It is the first time I try to to this within blade.这是我第一次尝试在刀片内进行此操作。

I know both components are inside a PHP page, but I was hoping that perhaps Vue might still be able to communicate through the app.js somehow.我知道这两个组件都在一个 PHP 页面中,但我希望 Vue 可能仍然能够以某种方式通过 app.js 进行通信。

Thanks!谢谢!

Might be a race condition where componentOne is emitting the event before componentTwo is mounted so it's not listening yet.可能是一种竞争条件,其中 componentOne 在安装 componentTwo 之前发出事件,因此它尚未侦听。 Try moving your $on call to created() or even putting a timeout on the $emit.尝试将您的 $on 调用移动到 created() 或什至在 $emit 上设置超时。 I might be misunderstanding your flow though.不过,我可能误解了你的流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM