首页 > 解决方案 > 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>

标签: laravelvue.js

解决方案


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 中使用单引号!


推荐阅读