我正在将一个数组从我的 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')
})();
关于javascript - 拉维 |如何在 JavaScript 中访问 session 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683692/