[英]Simple List Rendering in Vue with finding Index and passing props
so I do the beginning todo list stuff.所以我做开始的待办事项列表的东西。 I have this array
我有这个数组
state() {
return {
news: [
{
id: 1,
title: "Titel 1",
text: "Beispieltext 1"
},
{
id: 2,
title: "Titel 2",
text: "Beispieltext 2"
}
]
}
},
I want to list items (NewsItem) in a list (NewsItemList) for every entry in the news-array.我想为新闻数组中的每个条目在列表(NewsItemList)中列出项目(NewsItem)。 As simple as that.
就如此容易。
This is my NewsItem这是我的新闻项目
<template>
<div class="newsitem">
<h1
v-for="nachricht in news"
:key="nachricht.id"
>{{nachricht.title}}</h1>
<p
v-for="nachricht in news"
:key="nachricht.id"
>{{nachricht.text}}</p>
</div>
</template>
<script>
export default {
data() {
return {};
}
};
</script>
And this is my NewsItemList这是我的 NewsItemList
<template>
<ul>
<li
v-for="nachricht in news"
:key="nachricht.id"
>
<NewsItem />
</li>
</ul>
</template>
<script>
import NewsItem from "@/components/NewsItem";
export default {
components: {
NewsItem,
},
computed: {
news() {
return this.$store.getters.getNewsList;
}
}
};
</script>
I want to map through my array in NewsItemList and give the information as props to my NewsItem.我想通过我在 NewsItemList 中的数组 map 并将信息作为道具提供给我的 NewsItem。 What is the simplest way?
最简单的方法是什么?
In React, I needed to find the index with the findIndex() function.在 React 中,我需要使用 findIndex() function 来查找索引。 How can I do this in vue?
我如何在 vue 中做到这一点?
As I am just beginning, could you help me to find a simple way?由于我刚刚开始,你能帮我找到一个简单的方法吗? Thank you!
谢谢!
NewsItem新闻项目
<template>
<div class="newsitem">
<h1>{{ title }}</h1>
<p>{{ text }}</p>
</div>
</template>
<script>
export default {
props: {
title: String,
text: String,
},
data() {
return {}
},
}
</script>
Now you can use that in your NewsItemList现在您可以在 NewsItemList 中使用它
<template>
<ul>
<li v-for="nachricht in news" :key="nachricht.id">
<NewsItem :title="nachricht.title" :text="nachricht.text"/>
</li>
</ul>
</template>
If I understood you correctly, you just need to pass prop with news item object:如果我理解正确,您只需要使用新闻项 object 传递道具:
Vue.component('NewsItem', { template: ` <div class="newsitem"> <h1 >{{item.title}}</h1> <p>{{item.text}}</p> </div> `, props: ['item'] }) new Vue({ el: "#demo", data() { return { news: [ { id: 1, title: "Titel 1", text: "Beispieltext 1" }, { id: 2, title: "Titel 2", text: "Beispieltext 2" } ] } }, })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="demo"> <ul> <li v-for="nachricht in news":key="nachricht.id" > <news-item:item="nachricht"></news-item> </li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.