I want to pass an object from Laravel to Vue.js, then use it in :v-for="option in this.options"
. I am able to pass the object from my Laravel blade to Vue.js ponent and display it in the console, but when I want to use the object in v-for
I get this error:
TypeError: Cannot use 'in' operator to search for 'undefined' in [{"id":1,"poll_id":1,"content":"Black","created_at":"2019-12-15 02:53:52","updated_at":"2019-12-15 02:53:52"},{"id":2,"poll_id":1,"content":"Blue","created_at":"2019-12-15 02:53:52","updated_at":"2019-12-15 02:53:52"}]
at Proxy.render (app.js:48972)
at VueComponent.Vue._render (app.js:52735)
at VueComponent.updateComponent (app.js:53251)
at Watcher.get (app.js:53662)
at new Watcher (app.js:53651)
at mountComponent (app.js:53258)
at VueComponent../node_modules/vue/dist/vuemon.dev.js.Vue.$mount (app.js:58228)
at VueComponent../node_modules/vue/dist/vuemon.dev.js.Vue.$mount (app.js:61113)
at init (app.js:52315)
at createComponent (app.js:55157)
Vue.JS ponent
<template>
<div :v-for="option in this.options">
<div v-if="option" class="option mt-4">
{{ option }}
</div>
</div>
</template>
<script>
export default {
props: [
'id', 'options'
],
mounted() {
this.options = JSON.parse(this.options);
console.log(this.options);
},
}
</script>
Laravel blade
<options :id="{{ $poll->id }}" :options="'{{ json_encode($poll->options) }}'"></options>
console.log(this.options)
returns
(2) [{…}, {…}, __ob__: Observer]
0: {…} // id, poll_id, content, created_at, updated_at
1: {…} // id, poll_id, content, created_at, updated_at
length: 2
__ob__: Observer {value: Array(2), dep: Dep, vmCount: 0}
__proto__: Array
I want to pass an object from Laravel to Vue.js, then use it in :v-for="option in this.options"
. I am able to pass the object from my Laravel blade to Vue.js ponent and display it in the console, but when I want to use the object in v-for
I get this error:
TypeError: Cannot use 'in' operator to search for 'undefined' in [{"id":1,"poll_id":1,"content":"Black","created_at":"2019-12-15 02:53:52","updated_at":"2019-12-15 02:53:52"},{"id":2,"poll_id":1,"content":"Blue","created_at":"2019-12-15 02:53:52","updated_at":"2019-12-15 02:53:52"}]
at Proxy.render (app.js:48972)
at VueComponent.Vue._render (app.js:52735)
at VueComponent.updateComponent (app.js:53251)
at Watcher.get (app.js:53662)
at new Watcher (app.js:53651)
at mountComponent (app.js:53258)
at VueComponent../node_modules/vue/dist/vue.mon.dev.js.Vue.$mount (app.js:58228)
at VueComponent../node_modules/vue/dist/vue.mon.dev.js.Vue.$mount (app.js:61113)
at init (app.js:52315)
at createComponent (app.js:55157)
Vue.JS ponent
<template>
<div :v-for="option in this.options">
<div v-if="option" class="option mt-4">
{{ option }}
</div>
</div>
</template>
<script>
export default {
props: [
'id', 'options'
],
mounted() {
this.options = JSON.parse(this.options);
console.log(this.options);
},
}
</script>
Laravel blade
<options :id="{{ $poll->id }}" :options="'{{ json_encode($poll->options) }}'"></options>
console.log(this.options)
returns
(2) [{…}, {…}, __ob__: Observer]
0: {…} // id, poll_id, content, created_at, updated_at
1: {…} // id, poll_id, content, created_at, updated_at
length: 2
__ob__: Observer {value: Array(2), dep: Dep, vmCount: 0}
__proto__: Array
Share
Improve this question
asked Dec 15, 2019 at 21:21
Artur NawrotArtur Nawrot
4931 gold badge8 silver badges19 bronze badges
1 Answer
Reset to default 5There's a few mistakes in this block:
<div :v-for="option in this.options">
<div v-if="option" class="option mt-4">
{{ option }}
</div>
</div>
You have to fix the following:
- Change
:v-for
to justv-for
Remove
this.
fromthis.options
because you only should usethis.
in thescript
areaPut this whole block inside another
<div>
because there should be only 1 element inside<template>
tag. So since you're using a loop, multiple elements will be rendered