【发布时间】:2020-03-26 08:02:52
【问题描述】:
我有一个 Vue + nuxt.js 应用程序,它使用 Highcharts 呈现几个页面。这些图表是由一个动态组件创建的,该组件将 Web 服务 URL 作为参数。如何将此类页面缓存大约 1 天?
我找到了这两个链接,但这些只是指组件缓存,而不是整个页面。组件缓存将根据“名称”缓存组件,并且会阻碍动态缓存采用参数的缓存?因此,这种方法看起来不适合我。
关于如何缓存我的页面有什么建议吗?
我使用 URL 参数调用动态组件的示例页面:
<template>
<keep-alive>
<chart :url="this.$axios.defaults.baseURL + 'api/analytics/age'" keep-alive/>
</keep-alive>
</template>
<script>
import chart from '~/components/analytics/chart'
export default {
components: {
chart,
},
}
</script>
动态组件的一个例子,它接受参数,然后进行 Web 服务调用以获取用于渲染图表的数据。
<template>
<highcharts v-if="isChartDataLoaded" :options="chartOptions"></highcharts>
</template>
<script>
import axios from 'axios';
import {Chart} from 'highcharts-vue'
import Highcharts3D from 'highcharts/highcharts-3d'
import Highcharts from 'highcharts'
if (typeof Highcharts === 'object') {
Highcharts3D(Highcharts);
}
export default {
name: 'chart',
props: ['url'],
serverCacheKey: props => props.url,
components: {
highcharts: Chart
},
data() {
return {
isChartDataLoaded: false,
chartOptions: {
title: {
text: ''
},
tooltip: {
pointFormat: '{point.percentage:.2f}%',
},
chart: {
type: 'pie',
options3d: {
enabled: true,
alpha: 50,
},
},
series: [{
name: '',
data: [1],
tooltip: {
valueDecimals: 0
},
animation: false
}],
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
innerSize: '30%',
depth: 100,
dataLabels: {
enabled: true,
percentageDecimals: 2,
color: '#002a52',
connectorColor: '#002a52',
formatter: function () {
return '<b>' + this.point.name + '</b>: ' + this.percentage.toFixed(2) + ' %';
}
}
}
},
credits: {
enabled: false
},
exporting: {
buttons: {
printButton: {
enabled: false
},
contextButton: {
enabled: false
}
}
},
}
};
},
mounted() {
axios.post(this.url, {
locale: this.$route.query.locale ? this.$route.query.locale : this.$i18n.locale
}).then(response => {
this.chartOptions.series[0].data = response.data;
this.isChartDataLoaded = true
}).catch(e => {
console.log(e)
})
},
}
</script>
【问题讨论】:
标签: javascript vue.js caching nuxt.js