当前位置:网站首页>Laravel文档阅读笔记-Rendering JSON(对JS变量进行赋值)
Laravel文档阅读笔记-Rendering JSON(对JS变量进行赋值)
2022-08-09 07:16:00 【IT1995】
这里适用的场景是把Json数据赋值给view中JS变量,如下例子:
<script>
var app = <?php echo json_encode($array); ?>;
</script>
也可以使用Illuminate\Support\Js::from这个去代替json_encode,这个from接收一个参数,可以接收的参数和上面的json_encode是一致的,代码如下:
<script>
var app = {
{ Illuminate\Support\Js::from($array) }};
</script>
在最新(2022-06-23 10:17:43)的版本中Laravel提供了最优雅的方法去赋值,但都需要在Blade模板中使用:
<script>
var app = {
{ Js::from($array) }};
</script>
我这里是这样的:
<script type="text/javascript">
let subwayStation = <?php if (isset($subwayStation)) {
echo json_encode($subwayStation);
} ?>;
......
......
</script>
用log打印下。
赋值成功。
边栏推荐
猜你喜欢
随机推荐
ImportError: cannot import name ‘imresize‘
重要消息丨.NET Core 3.1 将于今年12月13日结束支持
tianqf的解题思路
unity第一课
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
链表专项练习(三)
Transaction concluded
postgresql窗口功能
高项 04 项目整体管理
Invoker 2019CCPC Qinhuangdao Station I Question Simple DP
(error) NOAUTH Authentication required.
The maximum validity period of an SSL certificate is 13 months. Is it necessary to apply for multiple years at a time?
查看日志常用命令
字节也开始缩招了...
Lottie系列一:介绍与使用
jmeter concurrency and some limitations of the press
Lottie系列四:使用建议
car-price-deeplearning-0411
sklearn数据预处理
975. 奇偶跳 有序集合