Use the variable process.env.VUE APP BASE API

Use the variable process.env.VUE_APP_BASE_API to get the value.

Use it directly in the js file

const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 10000,
})

  1. Vue2.0 page writing method
    is defined by data

<template>
    <div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                uploadUrl: process.env.VUE_APP_BASE_API,
            }
        }
    }
</script>

use computed()

<template>
    <div>
    </div>
</template>

<script>
    export default {
        computed: {
            VUE_APP_BASE_API(){
                return process.env.VUE_APP_BASE_API
            }
        }
    }
</script>

  1. Vue3.0 page writing

Vue3.0 uses a writing method of setup()

<template>
    <div>
    </div>
</template>

<script>
    export default {
        setup() {
            return {
                uploadUrl:'process.env.VUE_APP_BASE_API'
            }
        }
    }
</script>

or use computed()

<template>
    <div>
    </div>
</template>

<script>
    import { computed } from 'vue'
    export default {
        setup() {
            const VUE_APP_BASE_API = computed(()=>{
                return process.env.VUE_APP_BASE_API
            })
            return {
                VUE_APP_BASE_API
            }
        }
    }
</script>

Reprinted from: /zspt/p/13829751.html

Leave a Comment

Your email address will not be published. Required fields are marked *