我正在尝试调用一个对象作为 Prop 。 Auth::user()->car
是我的 Car 模型,但它可以为 null。
现在,在 vue 中,我试图将它传递给我的组件:
<my-comp :car="{{ Auth::user()->car }}"> </my-comp>
如果 Car 对象存在,它就起作用。但是,如果它为 null,vue 会抛出错误
[Vue warn]: Failed to generate render function:
SyntaxError: Unexpected token } in
我知道我可以使用 return view()->with(..
返回 $car
,但是我很好奇为什么这种方法不起作用以及我是什么做错了。
我尝试过的其他方式:
<my-comp :car="{{ Auth::user()->car ? Auth::user()->car : null }}"> </my-comp>
<my-comp car="{{ Auth::user()->car }}"> </my-comp>
最佳答案
如果您使用空字符串,模板将无法编译。
您没有使用 <my-comp :car=""></my-comp>
设置空字符串.
正确的方法是:<my-comp :car="''"></my-comp>
.
回想一下 v-bind
需要 javascript 表达式。
所以你可以试试:
<my-comp :car="{{ Auth::user()->car ? Auth::user()->car : 'null' }}"> </my-comp>
然后你可以在vue中解码那个空字符串
关于laravel - 将 null Laravel 模型值作为 Prop 传递时 Vue 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47803578/