gpt4 book ai didi

javascript - Vue.JS - 类型错误 : Cannot use 'in' operator to search for 'undefined' in Laravel Object

转载 作者:行者123 更新时间:2023-11-30 13:45:50 27 4
gpt4 key购买 nike

我想将一个对象从 Laravel 传递到 Vue.js,然后在 :v-for="option in this.options" 中使用它。我能够将对象从 Laravel Blade 传递到 Vue.js 组件并将其显示在控制台中,但是当我想在 v-for 中使用该对象时,我收到此错误:

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.common.dev.js.Vue.$mount (app.js:58228)
at VueComponent../node_modules/vue/dist/vue.common.dev.js.Vue.$mount (app.js:61113)
at init (app.js:52315)
at createComponent (app.js:55157)

Vue.JS 组件

<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) 返回

(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

最佳答案

此 block 中有一些错误:

<div :v-for="option in this.options">
<div v-if="option" class="option mt-4">
{{ option }}
</div>
</div>

您必须修复以下问题:

  • 更改:v-for到只是v-for
  • 删除this.来自this.options因为你只应该使用this.script面积

  • 将整个 block 放入另一个 <div> 中因为 <template> 里面应该只有 1 个元素标签。因此,由于您使用的是循环,因此将渲染多个元素

关于javascript - Vue.JS - 类型错误 : Cannot use 'in' operator to search for 'undefined' in Laravel Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59348171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com