拉拉维尔 | 如何在 JavaScript 中访问会话数组?

Sap*_*aik 3 javascript arrays laravel laravel-blade laravel-5.4

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

这是控制器功能:

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

在我看来:

<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>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我试图访问数组值的方式显然有问题 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;
}
Run Code Online (Sandbox Code Playgroud)

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

试图获取非对象的属性(视图:C:\xampp\htdocs\financetest1\resources\views\layouts\master.blade.php)(视图:C:\xampp\htdocs\financetest1\resources\views\layouts\ master.blade.php)

任何人都可以帮我解决这个问题吗?

Mar*_*lln 5

您应该将 PHP 和 Javascript 代码分开。data在您的 HTML 中使用属性并在您的 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>
Run Code Online (Sandbox Code Playgroud)

然后在你的 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;
    }
})();
Run Code Online (Sandbox Code Playgroud)

您可以通过执行以下操作来缩短您的 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')
})();
Run Code Online (Sandbox Code Playgroud)

阅读更多内容:HTMLElement.dataset条件(三元)运算符