Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting env data in vue component

Tags:

laravel

vue.js

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?

like image 257
mafortis Avatar asked Jun 09 '26 08:06

mafortis


2 Answers

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

like image 71
Илья Зеленько Avatar answered Jun 11 '26 21:06

Илья Зеленько


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 ....

like image 26
saber tabatabaee yazdi Avatar answered Jun 11 '26 20:06

saber tabatabaee yazdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!