我實施了評論中提出的更改。它現在正在作業!非常感謝大家的幫助
------------ 初始幫助請求如下: -----------
我有一個用作主頁的 Home.vue 父組件。在此頁面上,我發出一個獲取帖子陣列的請求。我嘗試使用 v-for 遍歷帖子陣列并每次發送一個新的帖子物件。
我的問題如下:
- 陣列被完整發送,因此只生成一個 Post 模板......
提前感謝您的時間,我做了一些研究,但沒有任何成功。
找到下面的 Home.vue 父組件
<template>
<div >
<div v-for="(post, index) in posts" :key="index">
<ImageArticle
:articledata="post"
/>
</div>
</div>
</template>
<script>
import ImageArticle from "../components/imageArticle"
import { mapState } from "vuex"
export default {
name: 'Home',
components: {
ImageArticle,
},
data() {
return {
posts : [],
}
},
computed: {
...mapState({
UserName: "UserName",
UserLogin: "UserLogin",
UserEmail: "UserEmail",
UserPublications: "UserPublications",
UserFriends: "UserFriends"
})
},
created() {
this.getAllPosts()
},
methods:{
getAllPosts () {
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
fetch("http://localhost:3000/api/post/", requestOptions)
.then(response => response.text())
.then((result) => {
this.posts = JSON.parse(result);
console.log(this.posts);
})
.catch(error => console.log('error', error));
}
}
}
</script>
在子組件下方找到我嘗試將 post 物件作為道具發送到的子組件。(我洗掉了很多代碼,使其更具可讀性)
<template>
<div>
<article >
<h4 >Kaerbran {{articleData.Post_Comment}}</h4>
</article>
</div>
</template>
<script>
import ModalBoxActionArticle from '../components/modalBoxActionArticle'
export default {
name: 'ImageArticle',
props : ['articledata'],
data() {
return {
articleData : this.articledata,
}
}
}
</script>
在我發送的陣列下方找到。我想為每個 Post_ID 創建一個新模板。
"posts": [
{
"Post_ID": "48be1774-4563-478b-9201-56ab2887d9a1",
"Post_Picture": "placeholder.png",
"Post_Location": "ParisII",
"Post_Date_published": "2021-10-22T17:43:10.281Z",
"Post_Date_modified": "2021-10-22T17:43:10.281Z",
"Post_Comment": "un commentaire",
"Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"
},
{
"Post_ID": "88ce1c98-ad16-4987-82c9-f8a9abff7f33",
"Post_Picture": "placeholder.png",
"Post_Location": "ParisII",
"Post_Date_published": "2021-10-22T17:44:12.985Z",
"Post_Date_modified": "2021-10-22T17:44:12.985Z",
"Post_Comment": "un commentaire",
"Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"
},
{
"Post_ID": "b43abd59-ce16-4c0e-a3e6-03402a35ecfc",
"Post_Picture": "placeholder.png",
"Post_Location": "ParisIII",
"Post_Date_published": "2021-10-22T17:45:23.013Z",
"Post_Date_modified": "2021-10-22T17:45:23.013Z",
"Post_Comment": "un commentaire",
"Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"
},
{
"Post_ID": "f2737804-9493-42d9-b425-51334c3a360a",
"Post_Picture": "placeholder.png",
"Post_Location": "Paris",
"Post_Date_published": "2021-10-22T17:15:56.994Z",
"Post_Date_modified": "2021-10-22T17:15:56.994Z",
"Post_Comment": "un commentaire",
"Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"
},
{
"Post_ID": "fcd4d5a0-c771-4243-99d9-b374b00c2159",
"Post_Picture": "placeholder.png",
"Post_Location": "ParisIII",
"Post_Date_published": "2021-10-22T17:45:01.362Z",
"Post_Date_modified": "2021-10-22T17:45:01.362Z",
"Post_Comment": "un commentaire",
"Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"
}
]
}
uj5u.com熱心網友回復:
需要注意的幾點:
首先,為迭代元素傳遞鍵的方式是錯誤的。陣列的傳遞和索引會導致不可預測的結果或 ui 未更新。將這一行從
v-bind:key="{index}"
到
v-bind:key="post.Post_ID"
然后,您將資料傳遞給道具的方式也是錯誤的,從
v-bind:sendArticleData = post
到
v-bind:send-article-data="post"
甚至更簡單地:
:send-article-data="post"
在此處閱讀有關 Vue 中的 props 外殼的更多資訊
uj5u.com熱心網友回復:
如果我理解正確,請嘗試以下代碼段:
Vue.component('Child', {
template: `
<div >
<div >
<h4 >Kaerbran {{articleData.Post_Comment}}</h4>
</div>
</div>
`,
props : ['articledata'],
data() {
return {
articleData : this.articledata,
}
}
})
new Vue({
el: '#demo',
data() {
return {
posts: [
{"Post_ID": "48be1774-4563-478b-9201-56ab2887d9a1", "Post_Picture": "placeholder.png", "Post_Location": "ParisII", "Post_Date_published": "2021-10-22T17:43:10.281Z", "Post_Date_modified": "2021-10-22T17:43:10.281Z", "Post_Comment": "un commentaire1", "Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"},
{"Post_ID": "88ce1c98-ad16-4987-82c9-f8a9abff7f33", "Post_Picture": "placeholder.png", "Post_Location": "ParisII", "Post_Date_published": "2021-10-22T17:44:12.985Z", "Post_Date_modified": "2021-10-22T17:44:12.985Z", "Post_Comment": "un commentaire2", "Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"},
{"Post_ID": "b43abd59-ce16-4c0e-a3e6-03402a35ecfc", "Post_Picture": "placeholder.png", "Post_Location": "ParisIII", "Post_Date_published": "2021-10-22T17:45:23.013Z", "Post_Date_modified": "2021-10-22T17:45:23.013Z", "Post_Comment": "un commentaire3", "Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"},
{"Post_ID": "f2737804-9493-42d9-b425-51334c3a360a", "Post_Picture": "placeholder.png", "Post_Location": "Paris", "Post_Date_published": "2021-10-22T17:15:56.994Z", "Post_Date_modified": "2021-10-22T17:15:56.994Z", "Post_Comment": "un commentaire4", "Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"},
{"Post_ID": "fcd4d5a0-c771-4243-99d9-b374b00c2159", "Post_Picture": "placeholder.png", "Post_Location": "ParisIII", "Post_Date_published": "2021-10-22T17:45:01.362Z", "Post_Date_modified": "2021-10-22T17:45:01.362Z", "Post_Comment": "un commentaire5", "Post_Creator": "d4d2c7d4-e710-4842-9622-915ed21bdd71"}
]
}
},
})
Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<div class="home">
<div v-for="(post, index) in posts" :key="index">
<Child
:articledata="post"
class="home__component"/>
</div>
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368235.html
上一篇:Laravel強化,Vuex。陣列到字串轉換錯誤。FortifyServiceProvider.php第40行。發生了什么?
下一篇:獲取陣列的欄位值
