这是我的json的样子,我用v-for格式显示了一堆,我可以点击其中一个,我想用模态显示我点击的元素的数据。
[{
"id": 1,
"companyName": "test",
"image": "https://mmelektronik.com.pl/wp-content/uploads/2017/10/Insert-logo.jpg.png",
"location": "Warsaw",
"salary": "10000",
"skill": "Junior",
"tags": "test",
"jobDescription": "test",
"title": "UI Designer"
}
]
Now I want to access only jobDescription and display it in the modal.
b-modal(hide-footer="", :id="id")
template(#modal-title="")
| Information
.d-block.text-center
p {{ want the jobDescription here }}
b-button(variant="primary") Apply
这是我如何打开模态。
openModal(item) {
this.offer = item;
this.$bvModal.show(this.id);
}
组件
b-container
b-card.joblist__post.mt-2(
v-for="offer in filteredOffers",
:id="id"
:key="offer.id",
@click="openModal"
)
.d-flex
.joblist.d-flex.w-100
.joblist__info.d-flex.align-items-center
.company-logo.d-flex.align-items-center.mr-3
b-img.logo(:src="offer.image")
.joblist-name.d-flex.flex-column.text-left
h5.mb-0.font-weight-bold {{ offer.title }}
.located.d-flex.align-items-center.mt-2.justify-content-start
b-icon.mr-2(icon="geo-alt-fill")
p.m-0.text-secondary.joblist-span {{ offer.location }}
b-icon.mx-2(icon="person-fill")
p.m-0.text-secondary.joblist-span {{ offer.companyName }}
b-icon.mx-2(icon="bar-chart-fill")
b-modal(hide-footer="", :id="id")
template(#modal-title="")
| Information
.d-block.text-center
p {{offer.jobDescription}}
b-button(variant="primary") Ok
export default {
data() {
return {
search: "",
};
},
computed: {
...mapState({
offers: (state) => state.offer.offers,
loading: (state) => state.offer.loading
}),
filteredOffers(){
return this.offers.filter((offer) => {
return offer.title.match(this.search);
})
},
},
methods: {
...mapActions({
fetchOffers: "fetch",
}),
openModal(item) {
this.offer = item;
this.$bvModal.show(this.id);
}
},
mounted() {
this.fetchOffers();
this.id = this._uid;
},
};
2条答案
按热度按时间mi7gmzs61#
这里有一个简单的方法,在数据中设置一个
selectedItem
属性,如下所示:并在元素上添加一个on click,如下图所示,其中您将单击的对象赋给selectedItem:
然后简单地将selectedItem作为props传递给modal,这样当modal出现时,它将显示被单击的props
selectedItem
!**EDIT:**在您的情况下,您也应该从for循环中删除模态。另外,您不需要将
selectedItem
作为props传递给模态,因为您可以访问selectedItem
。uurity8g2#
将您的
data
更改为:在模板中使用以下内容:
并将
click
处理程序更改为:一旦
offer
首先在数据中定义并从点击中传递,来自评论的建议应该起作用。你的modal不应该在
v-for
里面,把它取出来,硬编码一个id:打开它: