Last active
February 21, 2019 10:13
-
-
Save Tenderfeel/a0fb7aaba5310d7340333b9456bc7a79 to your computer and use it in GitHub Desktop.
nuxt-childでkeep-aliveを有効にした時にdataを保持する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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