SoFunction
Updated on 2025-04-12

Easy way to configure vue-cli3 across domains

vue-cli3 cross-domain configuration:

In vue-resource data request, we generally modify the request method GET/POST to jsonp request method to achieve cross-domain.

However, for APIs that only support GET/POST request methods, modifying jsonp will cause an error. Cross-domain configuration is required.

(1) Create a configuration file in the file root directory, and the specific configuration is as follows:

={

//Change baseUrl: '/api' to baseUrl: '/'baseUrl:'/',
devServer:{
  '/api':{
    target:'/',
    changeOrigin:true,
    ws:true,
    pathRewrite:{
      '^/api':'' 
    }
     
  }
}}

(2) Create in the root directory of the file. Used as the development environment variable setting.

Set the variable VUE_APP_BASE_API=/api under the . file to assign the url of the proxy rewritten by devServer to VUE_APP_BASE_API

(3) Read the configuration variables and read them through .VUE_APP_XX(.VUE_APP_BASE_API).

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.