laravel |如何访问 JavaScript 中的会话数组?

laravel | How to access session array in JavaScript?

我正在将一个数组从我的控制器传递到一个视图。

这里是控制器函数:

$notification = array(
            'message' => 'Welcome Admin!', 
            'alert_type' => 'success',
        );
return redirect('/home')->with('notification', $notification);

在我看来:

<script>
  @if(Session::has('notification'))//this line works as expected

    var type = "{{ Session::get('alert_type', 'info') }}";
   //but the type var gets assigned with default value(info)
    switch(type){
        case 'info':
            toastr.info("{{ Session::get('message') }}");
            break;

        case 'warning':
            toastr.warning("{{ Session::get('message') }}");
            break;

        case 'success':
            toastr.success("{{ Session::get('message') }}");
            break;

        case 'error':
            toastr.error("{{ Session::get('message') }}");
            break;
    }
  @endif
</script>

如您所见,我尝试访问 var type = "{{ Session::get('alert_type', 'info') }}";

中的数组值的方式显然有问题

编辑- 1: 我试过

var type = "{{ Session::get('notification')->alert_type, 'info' }}";
switch(type){
    case 'info':
        toastr.info("{{ Session::get('notification')->message }}");
        break;

    case 'warning':
        toastr.warning("{{ Session::get('notification')->message }}");
        break;

    case 'success':
        toastr.success("{{ Session::get('notification')->message }}");
        break;

    case 'error':
        toastr.error("{{ Session::get('notification')->alert_type }}");
        break;
}

但现在我收到一条错误消息

Trying to get property of non-object (View: C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php) (View: C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php)

谁能帮我解决这个问题?

而不是{{ Session::get('message') }}

分别尝试 {{ Session::get('notification')->message }}{ Session::get('notification')->alert_type }}

您的会话消息 returns 数组,您需要使用数组键来获取消息而不是直接使用键。

您应该将 PHP 和 Javascript 代码分开。在 HTML 中使用 data 属性,并在 Javascript 代码中获取值。

例如这个HTML代码(我使用json_encode来支持换行):

<body {{ Session::has('notification') ? 'data-notification' : '' }} data-notification-type='{{ Session::get('alert_type', 'info') }}' data-notification-message='{{ json_encode(Session::get('message')) }}'>
    // ...
</body>

然后在你的 JS 文件中:

(function(){
    // Don't go any further down the script if [data-notification] is not set.
    if ( ! document.body.dataset.notification)
        return false;

    var type = document.body.dataset.notificationType;
    switch(type){
        case 'info':
            toastr.info(JSON.parse(document.body.dataset.notificationMessage));
            break;

        case 'warning':
            toastr.warning(JSON.parse(document.body.dataset.notificationMessage));
            break;

        case 'success':
            toastr.success(JSON.parse(document.body.dataset.notificationMessage));
            break;

        case 'error':
            toastr.error(JSON.parse(document.body.dataset.notificationMessage));
            break;
    }
})();

您可以通过这样做来缩短您的 JS:

(function(){
    // Don't go any further down the script if [data-notification] is not set.
    if ( ! document.body.dataset.notification)
        return false;

    var type = document.body.dataset.notificationType;
    var types = ['info', 'warning', 'success', 'error'];

    // Check if `type` is in our `types` array, otherwise default to info.
    toastr[types.indexOf(type) !== -1 ? type : 'info'](JSON.parse(document.body.dataset.notificationMessage));

    // toastr['info']('message') is the same as toastr.info('message')
})();

阅读更多:HTMLElement.dataset, Conditional (ternary) Operator

好的,在从@milo526 的回答中得到提示后,我做了更多研究并找到了解决方案。 @milo526 的解决方案试图将数组作为对象访问,因此 Laravel 嘎嘎作响。我这样做了,现在可以用了!

var type = "{{ Session::get('notification')['alert_type'], 'info' }}";
switch(type){
    case 'info':
        toastr.info("{{ Session::get('notification')['message'] }}");
        break;

    case 'warning':
        toastr.warning("{{ Session::get('notification')['message'] }}");
        break;

    case 'success':
        toastr.success("{{ Session::get('notification')['message'] }}");
        break;

    case 'error':
        toastr.error("{{ Session::get('notification')['message'] }}");
        break;
}