Skip to content

Instantly share code, notes, and snippets.

@jerryjappinen
Created September 2, 2018 09:58
Show Gist options
  • Save jerryjappinen/896d4878d8bb75d60f9a8b66cc760f9c to your computer and use it in GitHub Desktop.
Save jerryjappinen/896d4878d8bb75d60f9a8b66cc760f9c to your computer and use it in GitHub Desktop.
Nuxt.js layout mixin
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