Created
September 2, 2018 09:58
-
-
Save jerryjappinen/896d4878d8bb75d60f9a8b66cc760f9c to your computer and use it in GitHub Desktop.
Nuxt.js layout mixin
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
export default { | |
head () { | |
const page = (this.hasNuxtError ? 'error' : this.currentPage) | |
return { | |
// I have to do all of this here, since won't be combined | |
bodyAttrs: { | |
class: [ | |
'body-' + this.$options.name, | |
'body-page-' + page | |
].join(' ') | |
} | |
} | |
}, | |
data () { | |
return { | |
hasNuxtError: false | |
} | |
}, | |
mounted () { | |
this.$nuxt.$on('routeChanged', this.onRouteChange) | |
setTimeout(this.onRouteChange, 1) | |
}, | |
beforeDestroy () { | |
this.$nuxt.$off('routeChanged', this.onRouteChange) | |
}, | |
methods: { | |
onRouteChange () { | |
// This is not reactive, so we must manually look into it on route change | |
if (this.$nuxt.nuxt.err && !this.hasNuxtError) { | |
this.hasNuxtError = true | |
} else if (!this.$nuxt.nuxt.err && this.hasNuxtError) { | |
this.hasNuxtError = false | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment