将php参数传递给bladee中的vue函数



在我的刀片视图中。

@foreach($r_templates as $key => $rt)
<button @click="someFunc({{$rt['title'}})">click me</button>
@endforeach
// In my vue script
someFunc(title) { console.log(title); }

上面的代码似乎不起作用,也没有错误。

但当我将参数更改为'something'时,它就起作用了。

如何通过PHP variable to a vue function?

作为传递字符串,您需要将' '添加到此报价

试试这个

@foreach($r_templates as $key => $rt)
<button @click="someFunc( '{{$rt['title'}}' )">click me</button>
@endforeach

参数/值周围没有引号。

使用JSON函数确保正确的引号和转义:

'someFunc({{ Js::from($rt['title']) }})'

参考:https://laravel.com/docs/8.x/blade#rendering-json

最新更新