【发布时间】:2018-03-25 00:28:16
【问题描述】:
我正在beforeMount 内通过 AJAX 加载组件的数据。但是,每次我折叠 vue-strap accordion 元素时,它都会清除整个内容,迫使它每次都重新加载 AJAX。我尝试通过 if (this.cycles.length === 0) 将 AJAX 调用包装在一个检查数据是否已被填充的条件下,但显然它也会在手风琴崩溃时被清除。
这是我的父模板:
<template>
<accordion :one-at-atime="true" type="info">
<panel :is-open="index === 0" type="primary" :header="'Day ' + day.day" v-for="(day, index) in days" :key="day.id">
<accordion :one-at-atime="true" type="success">
<panel is-open type="success" header="Cycles">
<cycles
:day="day"
>
</cycles>
</panel>
</accordion>
</panel>
</accordion>
</template>
<script>
export default {
props: [
'plan'
],
data() {
return {
days: []
}
},
beforeMount: function () {
var self = this;
if (this.days.length === 0) {
axios.get('/plans/' + this.plan.id + '/days/data')
.then(function (response) {
self.days = response.data;
})
.catch(function (error) {
console.log(error);
});
}
}
}
</script>
这是包含循环的cycles.vue script that it loads repeatedly every time I collapse aday` 手风琴:
<template>
<accordion :one-at-atime="true" type="info">
<panel :is-open="index === 0" type="primary" :header="'Week ' + cycle.week + ': ' + cycle.name" v-for="(cycle, index) in cycles" :key="cycle.id">
<form v-on:submit.prevent="update">
....misc input fields here...
</form>
</panel>
</accordion>
</template>
<script>
export default {
props: [
'day'
],
data() {
return {
cycles: []
}
},
beforeMount: function () {
var self = this;
if (this.cycles.length === 0) {
axios.get('/plans/days/' + this.day.id + '/cycles/data')
.then(function (response) {
self.cycles = response.data;
})
.catch(function (error) {
console.log(error);
});
}
}
}
</script>
如何确保每次在手风琴上发生简单的显示/隐藏时不会重新加载数据?
【问题讨论】:
-
好电话,@RoyJ!真的有可能!
-
我尝试了
<keep-alive><cycles :day="day"></cycles></keep-alive>,但每次折叠day手风琴时仍会重新加载循环数据。我还尝试将原始加载点包装在父 Vue 模板的 Blade 模板中,但仍然不行。
标签: javascript vue.js vuejs2