我有用於顯示使用循環項的主要成分:如何在vuejs的組件打開時觸發一個方法?
<v-list-tile v-for="(item, index) in items" :key="item.title">
...
<report type="item.type"> </report>
</v-list>
的report
部件是用來報告在系統上濫用,並報告類型可取決於item
從父環而變化。
由於用戶不太可能定期使用report
,所以我只想在打開對話框(模式)時加載v-select
元素。
使用created
或mounted
觸發每次的report
分量被生成並且不是當report
部件被打開裝載方法。
是否有一種智能的方法來防止這種情況發生,只有在打開組件時纔會觸發report
中的加載方法。
=== Report.vue文件===
===此文件被加載在父組件
<template lang="html">
<v-dialog v-model="dialog" persistent max-width="800px" lazy>
<v-btn icon slot="activator">
<v-icon>error_outline</v-icon>
</v-btn>
<v-card>
<v-card-title>
<div class="headline"><v-icon large>error_outline</v-icon> Reporting</div>
</v-card-title>
<v-card-text>You are about to report the following {{ reportType }}: "<i>{{ reportContent.title }}</i>"
<v-container v-if="this.$store.getters['report/getLoadedState']" grid-list-md >
<v-layout wrap>
<v-flex xs12 sm12>
<v-select
label="Select a reason"
required
cache-items
:items="items"
item-text="title"
item-value="id"
></v-select>
</v-flex>
<v-flex xs12 sm12>
<v-text-field
label="Please provide additional information here"
multi-line></v-text-field>
</v-flex>
</v-layout>
</v-container>
<v-container v-else grid-list-md>
<v-layout>
<v-flex xs12 sm12>
Loading
</v-flex>
</v-layout>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="green darken-1" flat="flat" @click.native="cancel">Cancel</v-btn>
<v-btn color="green darken-1" flat="flat" @click.native="report">Report</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>
<script>
export default {
name: 'report',
data() {
return {
dialog: false,
items: this.$store.getters['report/getItems']
}
},
props: ['reportType', 'reportContent'],
methods: {
cancel() {
this.dialog = false
},
report() {
this.dialog = false
},
loadReasons (type) {
if (!this.$store.getters['report/getLoadedState']) {
this.$store.dispatch('report/setItems', type)
}
}
}
}
</script>
<style lang="css" scoped>
</style>
PS 1:我不使用jQuery和不打算使用它
PS 2:調用report
成分以外的方法是不是一種選擇,因爲我想最大限度地利用這一compenent的可重用性和唯一使用props
你可以分享你'report'組件定義:
該解決方案也可以在找到? – thanksd
@thanksd完成。添加該項目是使用vue-cli生成的,並且添加了vuetify。 –