我有用于显示使用循环项的主要成分:如何在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。 –