如何在 Vue 中将列表中的一个条目作为道具传递
How to pass one entry from a list as prop in Vue
假设我在数据中有以下列表:
data: {
todos: [
{ id: 1, title: "Learn Python" },
{ id: 2, title: "Learn JS" },
{ id: 3, title: "Create WebApp" }
]
}
现在我只想将 id 为 2 的条目传递给 prop:
<dynamic-prop :id=todos[2] :title="todos.title"> </dynamic-prop>
在 Vue 中可以实现类似的功能吗?
当然,您可以传递任何 data
。只是不要忘记添加引号并注意差一问题。所以如果你想在一个(零索引)数组中传递 second 元素,你可以这样写:
<dynamic-prop :id="todos[1].id" :title="todos[1].title"> </dynamic-prop>
假设我在数据中有以下列表:
data: {
todos: [
{ id: 1, title: "Learn Python" },
{ id: 2, title: "Learn JS" },
{ id: 3, title: "Create WebApp" }
]
}
现在我只想将 id 为 2 的条目传递给 prop:
<dynamic-prop :id=todos[2] :title="todos.title"> </dynamic-prop>
在 Vue 中可以实现类似的功能吗?
当然,您可以传递任何 data
。只是不要忘记添加引号并注意差一问题。所以如果你想在一个(零索引)数组中传递 second 元素,你可以这样写:
<dynamic-prop :id="todos[1].id" :title="todos[1].title"> </dynamic-prop>