gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'push' of undefined Vue JS

转载 作者:行者123 更新时间:2023-12-02 23:27:44 24 4
gpt4 key购买 nike

我有一个组件正在尝试将数据推送到另一个子组件,Abit 在这里感到困惑,因为我是 Vue.js 的新手。我尝试将子组件导入主组件,但添加项目的推送导致了问题?有什么想法吗?

我收到错误:

类型错误:无法读取未定义的属性“push”

我不明白为什么或在哪里?

主要成分:

<template>
<div class="container search">
<list></list>

<div class="jumbotron">
<h1 class="display-4">{{title}}</h1>
<p class="lead">{{intro}}</p>
<hr class="my-4">
<p v-if="validated" :class="errorTextClass">Enter a valid search term</p>

<button
type="button"
class="btn btn-primary btn-lg mb-3"
v-on:click="refreshPage"
v-if="result.length > 1"
>
<font-awesome-icon icon="redo"/>Start again
</button>
<input
class="form-control form-control-lg mb-3"
type="search"
placeholder="Search"
aria-label="Search"
v-model="search"
required
autocomplete="off"
id="search"
>

<div v-for="(result, index) in result" :key="index">
<div class="media mb-4">
<img
:src="resizeArtworkUrl(result)"
alt="Album Cover"
class="album-cover align-self-start mr-3"
>
<div class="media-body">
<h4 class="mt-0">
<button
type="button"
class="btn btn-primary btn-lg mb-3 float-right"
v-on:click="addItem(result)"
>
<font-awesome-icon icon="plus"/>
</button>
<b>{{result.collectionName}}</b>
</h4>
<h6 class="mt-0">{{result.artistName}}</h6>
<p class="mt-0">{{result.primaryGenreName}}</p>
</div>
</div>
</div>

<div :class="loadingClass" v-if="loading"></div>

<button
class="btn btn-success btn-lg btn-block mb-3"
type="submit"
v-on:click="getData"
v-if="result.length < 1"
>
<font-awesome-icon icon="search"/>Search
</button>
</div>
</div>
</template>

<script>
import List from "../components/myList.vue";

export default {
name: "Hero",
props: {
List
},
components: {
List
},
data: function() {
return {
title: "Simple Search",
intro: "This is a simple hero unit, a simple jumbotron-style.",
subintro:
"It uses utility classes for typography and spacing to space content out.",
result: [],
errors: [],
search: "",
loading: "",
message: false,
isValidationAllowed: false,
loadingClass: "loading",
errorTextClass: "error-text"
};
},

watch: {
search: function(val) {
if (!val) {
this.result = [];
}
}
},

computed: {
validated() {
return this.isValidationAllowed && !this.search;
}
},

methods: {
getData: function() {
this.isValidationAllowed = true;
this.loading = true;
fetch(`xxxxxxxx`)
.then(response => response.json())
.then(data => {
this.result = data.results;
this.loading = false;
/* eslint-disable no-console */
console.log(data);
/* eslint-disable no-console */
});
},
refreshPage: function() {
this.search = "";
},
addItem: function(result) {
this.myList.push(result);
},

resizeArtworkUrl(result) {
return result.artworkUrl100.replace("100x100", "160x160");
}
}
};
</script>

<style>
.loading {
background-image: url("../assets/Rolling-1s-42px.gif");
background-repeat: no-repeat;
height: 50px;
width: 50px;
margin: 15px;
margin-left: auto;
margin-right: auto;
}

.error-text {
color: red;
}

.media {
text-align: left;
}

.album-cover {
width: 80px;
height: auto;
}
</style>

子组件:

<template>
<div class="mb-5 container">
<button type="button" class="btn btn-primary mt-2 mb-2 btn-block">
My List
<span class="badge badge-light">{{myList.length}}</span>
</button>
<ul class="list-group" v-for="(result, index) in myList" :key="index">
<li class="list-group-item">
<b>{{result.collectionName}}</b>
<h6 class="mt-0">{{result.artistName}}</h6>
<p class="mt-0">{{result.primaryGenreName}}</p>
</li>
</ul>
</div>
</template>

<script>

export default {
name: "List",

data: function() {
return {
myList: [],
result: []
};
}
}
</script>

最佳答案

数据应该始终从父组件流向子组件;子组件不应修改属于父组件的数据。

如果您希望主组件能够将项目推送到 myList,那么 myList 应属于主组件,并通过以下方式将其传递给子组件:一个 Prop 。

简化示例:

主要成分

<list :list="list"/>
<button @click="addItem">Add Item</button>
import List from './list.vue'

export default {
components: {
List
},
data() {
return {
list: []
}
},
methods: {
addItem() {
this.list.push('New Item')
}
}
}

列表组件

<ul>
<li v-for="item of list">{{ item }}</li>
</ul>
export default {
props: ['list']
}

关于javascript - 类型错误 : Cannot read property 'push' of undefined Vue JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56658127/

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