laravel - Blade 将 Collection 作为 String 而不是 Array
问题描述
通过 Blade 将 Collection 传递给 Vue,将作为 String 而不是 Array 接收。过去,通常它会作为 Array 接收。现在它作为 JSON 编码的字符串接收。有谁知道为什么?显然,一些旧的 Vue 文件仍会以 Array 形式接收。
示例代码:
view.blade.php
@section('content')
<div id="todos_view">
<vue-component-here
:todos="{{ $todosCollection }}"
/>
</div>
<script src="{{ mix('/js/test.js') }}"></script>
@endsection
TodoView.vue
<template>
</template>
<script>
export default {
props: {
todos: {
type: Array, //This one should work. But instead, it will receive JSON-encoded String
required: false
}, ...
}
}
</script>
解决方案
view.blade.php
@section('content')
<div id="todos_view">
<vue-component-here
:todos='@json($todosCollection)'
/>
</div>
<script src="{{ mix('/js/test.js') }}"></script>
@endsection
多田!
PS - 确保在@json 中使用单引号!
推荐阅读
- excel - 从单元格中提取一个字母并查找该值
- python - 合并 Pandas 数据框行的内容
- c - 为什么使用内存布局来运行 C 程序
- typescript - Azure 函数:在异步函数中使用 Apollo Azure 处理程序
- javascript - 如何在 Mirage js 中播种具有多态一对一关系的模型?
- angular - ReactiveForm 强制执行 asyncValidator
- c# - WPF OneWay 将 ObservableCollection 绑定到 Treeview
- java - Apache HTTPClient POST 到 REST 服务的方式与 cURL 不同
- c++ - WebView2 旧版本的示例应用程序
- sql-server - SQL- 将 Sum int 转换为 Decimal