简体   繁体   English

object 使用道具从刀片传递到 vue 组件时未定义

[英]object is undefind when passing from blade to vue component using props

I'm trying to pass an array from async method in the controller to a vue component using props through the blade, but I'm getting undefined when I try to console.log it in the vue component, this is my code:我正在尝试通过刀片使用 props 将数组从 controller 中的异步方法传递到 vue 组件,但是当我尝试在 vue 组件中使用 console.log 时我变得不确定,这是我的代码:

Controller: Controller:

    public function index(){


           $pool = Pool::create();

           $pool[] = async(function () {

                     return Rfm::all();

           })->then(function ($output) {

           $this->results=$output;
           });

           await($pool);

    return view ("/dashboard", ["data" => $this->results]);

Blade:刀:

   <div id="total_customers">
       <total-customers :data="{{$data}}"></total-customers>
   </div>

Vue component:视图组件:

export default {
props: ['data'],

data() {
    return {
        
    };
},
mounted() {
    console.log(this.data);
}
};

I've searched and read somewhere that using async in the controller would be the problem as the object is still empty when it render to vue component, but I'm not sure about that, am i doing it wrong?我已经搜索并阅读了某个地方,在 controller 中使用异步会成为问题,因为 object 在渲染到 vue 组件时仍然是空的,但我不确定,我做错了吗?

Binding props that way might work with strings and integers but doesn't work with array/object data: https://laravel.com/docs/9.x/blade#blade-and-javascript-frameworks以这种方式绑定道具可能适用于字符串和整数,但不适用于数组/对象数据: https://laravel.com/docs/9.x/blade#blade-and-javascript-frameworks

The following should work:以下应该工作:

<total-customers :data="{{ Js::from($data) }}"></total-customers>

Addition: Might want to look into https://inertiajs.com/ if you bind data to vue components in blade a lot, its a nicer way to render vue components with props另外:如果你经常将数据绑定到 blade 中的 vue 组件,可能需要查看https://inertiajs.com/ ,这是一种使用 props 渲染 vue 组件的更好方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM