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

pass php parameters to vue function inside bladee

在我的 blade 视图中。

@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?

您在 parameter/value 周围没有引号。

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

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

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

作为您的传递字符串,您需要添加' '这个引用

试试这个

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