最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.JS - TypeError: Cannot use 'in' operator to search for 'undefined' in Laravel O

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

There'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 just v-for
  • Remove this. from this.options because you only should use this. in the script area

  • Put 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论