将数组传递给 Laravel 视图并将该数组用作 VueJS 道具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36559391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass array to Laravel view and use that array as VueJS prop
提问by dericcain
I have an array variable $screenshots
that I am trying to pass to my Laravel view. Usually, I would use the @foreach
and loop through the array, but I want to pass the full array to a Vue component by defining it as a prop. I want to do this so that I can loop over the array in the component. I am getting the htmlentities() expects parameter 1 to be string, array given
error.
我有一个数组变量$screenshots
,我试图将它传递给我的 Laravel 视图。通常,我会使用@foreach
and 循环遍历数组,但我想通过将其定义为 prop 来将完整数组传递给 Vue 组件。我想这样做,以便我可以遍历组件中的数组。我收到htmlentities() expects parameter 1 to be string, array given
错误。
What is the proper way to do this with VueJS and Laravel?
使用 VueJS 和 Laravel 执行此操作的正确方法是什么?
Here is my blade template:
这是我的刀片模板:
@section('content')
<ticket-edit id="edit-ticket" single-ticket="{{ $ticket }}" screenshots="{{ $files }}">
</ticket-edit>
@endsection
Here is my custom component (different file):
这是我的自定义组件(不同的文件):
<script>
export default {
template: '#edit-ticket-template',
props: ['SingleTicket', 'screenshots'],
data: function() {
return {
ticket: [],
screenshots: []
};
},
methods: {
getTicket() {
return this.ticket = JSON.parse(this.SingleTicket);
},
getScreenshots() {
return this.screenshots = JSON.parse(this.files);
},
createNotes: function () {
var ticketNotes = $('.summernote');
ticketNotes.summernote({
height: 260,
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear', 'strikethrough']],
['fontsize', ['fontsize']],
['para', ['ul', 'ol']],
]
});
}
},
created: function() {
this.getTicket();
this.getScreenshots();
},
ready: function() {
this.createNotes();
}
}
</script>
EDIT:When I am adding attachments, I am using json_encode
to encode the path to the attachments. Then when I retrieve them, I run json_decode
in my model like so $files = json_decode($ticket->screenshots);
So my controller looks like this:
编辑:当我添加附件时,我使用json_encode
对附件的路径进行编码。然后当我检索它们时,我json_decode
像这样在我的模型中运行$files = json_decode($ticket->screenshots);
所以我的控制器看起来像这样:
public function edit($sub_domain, $id)
{
$ticket = Ticket::find($id);
$files = json_decode($ticket->screenshots);
return view('templates.tickets-single', compact('ticket', 'files'));
}
回答by Iannazzi
This works - it was hard to find this answer on the web so I hope it helps! You have to bind it.
这有效 - 在网上很难找到这个答案,所以我希望它有所帮助!你必须绑定它。
<edit-ticket-template
v-bind:SingleTicket="{{ json_encode($ticket) }}"
v-bind: screenshots ="{{ json_encode($files) }}"
>
</edit-ticket-template>
Yeah I don't think you need to json_encode the single ticket but you get the point.
是的,我认为您不需要对单张票进行 json_encode,但您明白了。
回答by Jeff
I think Blade calls htmlentities()
automatically when you write {{ $ticket }}
. Since $ticket
is not a string, it is erroring. Try {{ json_encode($ticket) }}
我认为 Bladehtmlentities()
在您编写时会自动调用{{ $ticket }}
。由于$ticket
不是字符串,因此出错。尝试{{ json_encode($ticket) }}
回答by danial dezfooli
You should use {!! json_encode($ticket) !!}}
你应该使用 {!! json_encode($ticket) !!}}