vuejs configuration: using a global variable?

后端 未结 3 1681
有刺的猬
有刺的猬 2021-01-02 06:28

This seems dumb, but I have it setup like this:

in config/index.js:

module.exports = {
    API_LOCATION: \'http://localhost:8080/api/\'
         


        
相关标签:
3条回答
  • 2021-01-02 06:38

    Import it.

    <script>
        import config from "../config"
    
        module.exports = {
            data: function() {
                return {
                    obj: null
                }
            },
            created: function() {
                this.$http.get(config.API_LOCATION + '/call').then(res => {
                    // Do some business
                }, res => {
                    // Handle some error
                });
            }
        }
    </script>
    

    Or just the location.

    <script>
        import { API_LOCATION } from "../config"
    
        module.exports = {
            data: function() {
                return {
                    obj: null
                }
            },
            created: function() {
                this.$http.get(API_LOCATION + '/call').then(res => {
                    // Do some business
                }, res => {
                    // Handle some error
                });
            }
        }
    </script>
    
    0 讨论(0)
  • 2021-01-02 06:38

    PROD: config/prod.env.js append your VAR='"value"'

    'use strict'
    module.exports = {
      NODE_ENV: '"production"',
      API_LOCATION: '"https://production URL"'
    }
    

    DEV: config/dev.env.js append your VAR='"value"'

    'use strict'
    const merge = require('webpack-merge')
    const prodEnv = require('./prod.env')
    
    module.exports = merge(prodEnv, {
      NODE_ENV: '"development"',
      API_LOCATION: '"http://localhost"'
    })
    

    Your variable will available in process.env.API_LOCATION or process.env.VAR_NAME

    0 讨论(0)
  • 2021-01-02 06:54

    Simply set ip path(or localhost) in local storage when login successfull and get value from local storage where you need through out the project. here how you set value in localstrage.

    // Set value in IpAdress localstorage.setItem('IpAddress','192.168.100.100:8080');

    // Get value from IpAddress localstorage.getItem('IpAddress');

    in my case whole path looks like: localstorage.getItem('IpAddress')+/api/controller/method|

    0 讨论(0)
提交回复
热议问题