javascript - 拉维 |如何在 JavaScript 中访问 session 数组?

标签 javascript arrays laravel laravel-5.4 laravel-blade

我正在将一个数组从我的 Controller 传递到一个 View 。

Controller 函数如下:

$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)

谁能帮我解决这个问题?

最佳答案

您应该将 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

关于javascript - 拉维 |如何在 JavaScript 中访问 session 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683692/

相关文章:

php - 尝试在 laravel 5.2 中记录我的查询

php - laravel 5 中用于聊天的 Socket.io

mysql - Laravel SQLSTATE[23000] : Integrity constraint violation: 1451 Cannot delete or update a parent row

javascript - 使用 Vuejs 2 更新动态 MathJax?

javascript - 使用 es6 将对象的特定 Prop 合并为一个?

java - 二元运算符的操作数类型错误 "*"

javascript - 使用 jQuery 从 JSON 树读取特定值

javascript - 带有 classList 的 querySelectorAll() 上的 addEventListener

JavaScript 排序数组,但保持第二个数组同步

python - 用列表中的索引替换数组中的值