刀片将Collection作为字符串而不是数组传递

时间:2019-07-13 05:11:04

标签: laravel vue.js

通过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>

1 个答案:

答案 0 :(得分:2)

view.blade.php

@section('content')
  <div id="todos_view">
    <vue-component-here
      :todos='@json($todosCollection)'
    />
  </div>

  <script src="{{ mix('/js/test.js') }}"></script>
@endsection

多田!

P.S。 -确保在@json中使用单引号!