简体   繁体   English

Vue TypeError:尝试更新 Object 属性时无法读取未定义的属性“_wrapper”

[英]Vue TypeError: Cannot read property '_wrapper' of undefined when attempting to update Object property

I am trying to use a button to update a property of an Object in Vue.我正在尝试使用按钮来更新 Vue 中 Object 的属性。 The Object is returned by an AJAX query to a database, and the isFetching boolean is then set to false , which attaches the containing div to the DOM. Object 由 AJAX 查询返回到数据库,然后将isFetching boolean 设置为false ,将包含的 div 附加到 DOM。 When I try and update the property, I get the following error:当我尝试更新属性时,出现以下错误:

TypeError: Cannot read property '_wrapper' of undefined

Below is my AJAX code:下面是我的 AJAX 代码:

axios.post("/api/myEndpoint", { id: router.currentRoute.query.id })
    .then((response) => {
        this.activities = response.data.activities;
        this.isFetching = false;
    })
    .catch((errors) => {    
        console.log(errors);
        router.push("/");    
    });          

Here is my HTML:这是我的 HTML:

<div v-if="isFetching">
  <h2>Loading data...</h2>
</div>
<div v-else>
    <div class="row no-gutters">
        <div class="col">
            <div class="d-flex flex-row justify-content-center">
                <h4>Activities</h4>
            </div>
            <div class="custom-card p-2">
                <div class="row no-gutters pb-4" v-for="(activity, index) in activities" 
                 :key="activity.stage_id">
                    <button v-if="!activity.is_removed" class="btn custom-btn" :class="{'hov': 
                     index == 0}" :disabled="index != 0" 
                     @click="markRemoved(activity)">Remove</button>
                </div>
            </div>
        </div>
    </div>
</div>

Finally, here is markRemoved() as called by the button's click listener:最后,这里是按钮的点击监听器调用的markRemoved()

markRemoved(a) {
  a.is_removed = true;
}

When I try and log a in markRemoved() the Object is logged to the console fine, exactly as expected.当我尝试登录a markRemoved()时,Object 完全按照预期记录到控制台。 Having stepped through it in the debugger, the exception is thrown at the point I try and update the is_removed property of the Object.在调试器中单步执行后,在我尝试更新 Object 的is_removed属性时抛出异常。

Does anyone know what I'm doing wrong here?有谁知道我在这里做错了什么?

Note: the id I pass to the AJAX query is a query parameter of Vue Router.注意:我传递给AJAX查询的id是Vue Router的查询参数。 This is also set correctly and passed as expected.这也正确设置并按预期传递。

Here is an example activity Object:这是一个示例activity Object:

{date_time_due: "2020-12-09T11:43:07.740Z"
 date_time_reached_stage: "2020-12-02T11:43:07.740Z"
 is_complete: true
 is_removed: false
 selected_option: "Some text"
 stage_id: 1
 stage_name: "Some text"}

The exception is only thrown on the first click of the button.仅在第一次单击按钮时引发异常。

Posting in case anybody else comes across this error in the future.发布以防其他人将来遇到此错误。

Vue requires exactly one root element within a single-file component's <template> tags. Vue 在单个文件组件的<template>标记中只需要一个根元素 I had forgotten about this and in my case had two <div> elements, shown one at a time, conditionally using v-if:我忘记了这一点,在我的例子中,有两个<div>元素,一次显示一个,有条件地使用 v-if:

<template>
    <div v-if="fetchingData">
        <h2>Loading data...</h2>
    </div>
    <div v-else>
        <!-- The rest of the component -->
    </div>
</template>

This caused problems with Vue's reactivity, throwing the error whenever I tried to update some part of the component.这导致了 Vue 的反应性问题,每当我尝试更新组件的某些部分时都会抛出错误。 After realising my mistake, I wrapped everything in a root <div> .意识到我的错误后,我将所有内容都包裹在一个根<div>中。 This solved the issue for me:这为我解决了这个问题:

<template>
    <div id="fixedComponent">
        <div v-if="fetchingData">
            <h2>Loading data...</h2>
        </div>
        <div v-else>
            <!-- The rest of the component -->
        </div>
    </div>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue TypeError:无法读取未定义的属性“_wrapper” - Vue TypeError: Cannot read property '_wrapper' of undefined TypeError:无法读取未定义的属性“_wrapper” - TypeError: Cannot read property '_wrapper' of undefined 类型错误:无法读取未定义的 vueJS 的属性“_wrapper” - TypeError: Cannot read property '_wrapper' of undefined vueJS Nativescript Vue TypeError:无法读取未定义的属性“ 0” - Nativescript Vue TypeError: Cannot read property '0' of undefined Vue TypeError:无法读取未定义的属性“loggedIn” - Vue TypeError: Cannot read property 'loggedIn' of undefined TypeError:无法读取 Vue 中未定义的属性“_modulesNamespaceMap” - TypeError: Cannot read property '_modulesNamespaceMap' of undefined in Vue 类型错误:无法读取未定义的属性“更新” - TypeError: Cannot read property 'update' of undefined 类型错误:尝试导航 javascript object 时无法读取未定义的属性“1” - TypeError: Cannot read property '1' of undefined when trying to navigate a javascript object Uncaught typeError:无法读取未定义的属性-对象 - Uncaught typeError: Cannot read property of undefined - object 未捕获的类型错误:无法读取未定义的属性“对象” - Uncaught TypeError: Cannot read property 'object' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM