0

In the HTML part of the application process.env is undefined or not exists.

Example:

<img v-if="newsData[0].post[0].featured_image.slug" :src=" `domain as string` + newsData[0].post[0].featured_image.slug " />

And this works, but if I set variable in a bind src in HTML part:

<img v-if="newsData[0].post[0].featured_image.slug" :src=" `${process.env.VUE_APP_IMG_ROOT}` + this.newsData[0].post[0].featured_image.slug " />

TypeError: can't access property "env", _ctx.process is undefined

As result env not exist.

Why?

1 Answer 1

1

Because process is available during compilation only, not at runtime.

The recommended way of accessing env variables in templates is trough a computed property:

<img 
  v-if="newsData[0].post[0].featured_image.slug" 
  :src=" `${imageRoot}` + this.newsData[0].post[0].featured_image.slug " />

computed: {
  imageRoot(){
    return process.env.VUE_APP_IMG_ROOT
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.