gpt4 book ai didi

javascript - Toastr 使用 Laravel session 变量作为警报消息

转载 作者:行者123 更新时间:2023-11-29 14:48:10 24 4
gpt4 key购买 nike

我想从 Bootstrap 警报切换到 Toastr 警报。我目前的工作设置是:

@if (Session::has('flash_notification.message'))

<div class="alert alert-{{ Session::get('flash_notification.level') }}">
<button type="button" class="close" data-dismiss="alert"
aria-hidden="true">&times;</button>
{{ Session::get('flash_notification.message') }}
</div>

@endif

但我现在正在努力在 JS 中访问 Laravel 的 session 变量。

实际的 JS 工作示例是:

$(document).ready(function() {

toastr.info('Page Loaded!');

});

});

但是,我想使用 Laravel 的 session 变量来包含不同的消息和警告框:

@if (Session::has('flash_notification.message'))

<script>
$(document).ready(function() {

toastr.options.timeOut = 4000;
toastr.{{ Session::get('flash_notification.level') }}('{{ Session::get('flash_notification.message) }}');

});
</script>

@endif

我遇到了各种错误,例如 unexpected ;。任何帮助将不胜感激。非常感谢。

最佳答案

由于您在 Blade 模板中工作,您可以使用 {{ var/function() }} 输出变量/函数的内容。如果你想要未转义的输出,你可以使用 {!! var/function() !!.

所以你的问题的解决方案是,你需要用 {{ }} 标签包围你的 php 代码:

@if (Session::has('flash_notification.message'))

<script>
$(document).ready(function() {

toastr.{{ Session::get('flash_notification.level') }}
('{{ Session::get('flash_notification.message') }}');

});
</script>

@endif

关于javascript - Toastr 使用 Laravel session 变量作为警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882964/

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