gpt4 book ai didi

javascript - 在 Laravel 5.7 事件监听器中运行 Javascript

转载 作者:行者123 更新时间:2023-11-29 20:46:42 27 4
gpt4 key购买 nike

我正在尝试在用户登录时运行一个 javascript 库 Noty。我已经注册了该事件并遵循了文档 here .但是我尝试使用 echo 来发送 javascript,但是代码没有按照我所看到的执行。工作流程是,在登录 Home 页面时,会弹出一条通知,表示欢迎。

这是我的EventServiceProvider:

class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'Illuminate\Auth\Events\Login' => ['App\Listeners\UserEventListener@onUserLogin']
];

/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();


}
}

用户事件监听器:

class UserEventListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{

}

public function onUserLogin($event) {
echo("<script>
$(document).ready(function() {
new Noty({
text: 'Welcome {{Auth::user()->name}}! 😁',
type:'info',
layout: 'centerRight',
timeout:2000
})
.on('onShow', function() {
var audio = new Audio('/sounds/appointed.mp3');
audio.play();
}).show();
});
</script>");
echo("adad");
}
}

最佳答案

用 Laravel 显示消息似乎是一种非常奇怪的方法。

您可以简单地在您的登录 Controller 中使用这一行

return redirect('your/path')->with('success', 'your message here');

在 Blade 模板的某处执行简单检查:

@if (\Session::has('success'))
<script>
$(document).ready(function() {
new Noty({
text: 'Welcome {{ Auth::user()->name }}',
type:'info',
layout: 'centerRight',
timeout:2000
})
.on('onShow', function() {
var audio = new Audio('/sounds/appointed.mp3');
audio.play();
}).show();
});
</script>
@endif

您可以将您的通知代码放在通用(例如布局)模板中,这样如果用户 session 中有消息,它会在任何地方触发通知

关于javascript - 在 Laravel 5.7 事件监听器中运行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54184388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com