Skip to content

Instantly share code, notes, and snippets.

@Tenderfeel
Last active February 21, 2019 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tenderfeel/a0fb7aaba5310d7340333b9456bc7a79 to your computer and use it in GitHub Desktop.
Save Tenderfeel/a0fb7aaba5310d7340333b9456bc7a79 to your computer and use it in GitHub Desktop.
nuxt-childでkeep-aliveを有効にした時にdataを保持する
<template>
<div>
<p>{{id}}</p>
<div>{{profile}}</div>
</div>
</template>
<script>
let cacheData = null;
export default {
name: 'user-index',
data() {
if (cacheData) {
return cacheData;
}
return {
id: null,
profile: null,
};
},
methods: {
load() {
let { data } = await this.$axios.get('your-api');
this.id = data.id;
this.profile = data.profile;
},
beforeDestory() {
// Called right before a Vue instance is destroyed. At this stage the instance is still fully functional.
cacheData = null;
},
created() {
// Called synchronously after the instance is created.
this.load();
},
deactivated() {
// Called when a kept-alive component is deactivated.
if (!cacheData) {
cacheData = JSON.parse(JSON.stringify(this._data));
}
},
activated() {
// Called when a kept-alive component is activated.
},
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment