Accessing context from this

Viewed 392

I have a middleware that exports context.isMobile. I can access it from layout like this:

    layout (ctx) {
        if(ctx.isMobile) {
            return 'mobile'
        } else if (ctx.isDesktop) {
            return 'default'
        }
    },

...but I can't access the context from data or computed. How do I get the context there?

1 Answers

You can access the context via this.$nuxt.context like this:

export default {
  data() {
    console.log(this.$nuxt.context)
    return { /*...*/ }
  },
  computed: {
    myProp() {
      console.log(this.$nuxt.context)  
      return 'foo'
    }
  }
}
Related