最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access config data in .js in Laravel (Not in blade) - Stack Overflow

programmeradmin5浏览0评论

I can access the config data in .blade using

{{ config('config.variable') }}

However, I have no idea how to access the config data in .js file. Can anyone give me some suggestion?

I can access the config data in .blade using

{{ config('config.variable') }}

However, I have no idea how to access the config data in .js file. Can anyone give me some suggestion?

Share Improve this question edited Nov 13, 2019 at 9:50 Alessio Cantarella 5,2113 gold badges29 silver badges36 bronze badges asked Nov 13, 2019 at 9:49 Wei KangWei Kang 1832 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
  1. Type 1

    You can use the below code to access environment variables in your .env in js.

    var name = '{{ env('CONFIG_FILE_NAME')}}';
    console.log(name);
    
  2. Type 2

    Or else You may inject environment variables into your webpack.mix.js script by prefixing the environment variables in your .env file with MIX_.

    process.env.MIX_CONFIG_FILE_NAME
    

    Please Refer Documentation for further reference.

Javascript performed on client-side, and .blade.php on server-side. So, you can only write something like

<script>
var cfg = {{ config('config.variable') }};
</script>

in your .blade.php file to make it accessable from .js files. Or using method that is mentioned in the documentation by providing MIX_ prefix to variables in the .env file.

发布评论

评论列表(0)

  1. 暂无评论