3

How can I have .env data such as APP_NAME in my components?

Let say I want to show to users Welcome to {{APP_NAME}}

UPDATE

Base on this document I've made changes in my env file and like:

MIX_APP_NAME=Laravel

and added this to my component script:

data() {
  return {
    app_name: process.env.MIX_APP_NAME,
  }
},

Now I can have my app name in my component but the issue is I want to use it in bootstrap tooltip and there gives me this error:

- title=".... by {{app_name}}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">.

My code:

<span data-toggle="tooltip" data-placement="top" title="... {{app_name}}"></span>

Any idea?

4
  • title="`text ${app_name}`" Commented Aug 12, 2018 at 6:33
  • @ИльяЗеленько it doesn't give error but also print like ${app_name} in my view Commented Aug 12, 2018 at 6:34
  • 1
    sorry: :title="`text ${app_name}`" Commented Aug 12, 2018 at 6:35
  • @ИльяЗеленько that will do it :) please share it as answer i'll accept it Commented Aug 12, 2018 at 6:36

4 Answers 4

3

First add to env file:

MIX_APP_NAME=Laravel

and add this to your component script:

data() {
  return {
    app_name: process.env.MIX_APP_NAME,
  }
},

Now you can use it like this:

 <div :title="`text ${app_name}`"></div>

Or:

{{ app_name }}

Source

Sign up to request clarification or add additional context in comments.

1 Comment

thats worked for me without any require in webpack.mix ... just add a new variable in env file with this prefix : MIX_ but need to restart php artisan serve and also restart npm run watch....
2

thats worked for me without adding any require in webpack.mix

... just add a new variable in env file with this prefix : MIX_

BUT need to restart with php artisan serve and also restart with npm run watch ....

Comments

1

For Laravel 10.x:

You may inject environment variables into your JavaScript by prefixing them with VITE_ in your application's .env file:

VITE_APP_NAME=http://example.com

You may access injected environment variables via the import.meta.env object:

import.meta.env.VITE_APP_NAME

You can find the laravel documentation

Comments

0

you can directly use it as process.env.APP_NAME

mounted(){
   this.appName=process.env.APP_NAME
}

5 Comments

i found this as well laravel.com/docs/5.6/mix#environment-variables but it gives error on npm run watch
Are you using vue-cli?
How are you setting the variable in environment, maybe you are setting it in wrong way.
MIX_APP_NAME=Laravel
Updated my question please check it

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.