Nuxt.js: can we access context object from a vue component ?

Created on 7 Feb 2018  路  11Comments  路  Source: nuxt/nuxt.js

can we access context object in vue component?

This question is available on Nuxt.js community (#c2418)

Most helpful comment

What about "isDev"? I'd like to access that and there seems no way to do that but from the page's, asyncData block.

Answering this myself. You can access an environmental variable with process.env.NODE_ENV in a component.

All 11 comments

What object are you trying to access? You already have access to all variables, for example

this.$store
this.$route
// etc

What about "isDev"? I'd like to access that and there seems no way to do that but from the page's, asyncData block.

Answering this myself. You can access an environmental variable with process.env.NODE_ENV in a component.

Hi, how can access in script section? I tried "console.log($route.name)", but got error: Cannot read property 'middleware' of undefined.

@nurdus show some code and specify what you mean by script section.

<script>
// imports ...
console.log(this.$route.name) // doesn't work, I think it doesn't have to work
export default {
  data () {
    console.log(this.$route.name) //work
  }
}
</script>

@nurdus you can't access anything regarding the route outside the export, Vue may not even be initialized there AFAIK. Why would you need to do that anyway? Maybe you need a different approach for what you're trying to achive.

I've tried testing, it was my mistake :) Thanks.

According to this doc, isDev is available on client and server.
https://nuxtjs.org/api/context

How do you access it on client?

@blowsie have you tried process.isDev or process.env.isDev?

Quote from @manniL via discord.

So, the context object is available in the asyncData and fetch functions (as parameter)
You also can access it in nuxtServerInit
And in plugins as well

https://codesandbox.io/s/m3xqj4joky

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VincentLoy picture VincentLoy  路  3Comments

vadimsg picture vadimsg  路  3Comments

mattdharmon picture mattdharmon  路  3Comments

danieloprado picture danieloprado  路  3Comments

msudgh picture msudgh  路  3Comments