vue2 项目配置上线

本文详细介绍了Vue.js项目中的配置,包括dev环境的静态资源路径、代理表设置,用于实现API接口的转发。同时,讲解了axios的配置,确保跨域请求时携带cookie。在main.js中引入axios并使用vue-axios插件。最后,阐述了将项目部署到Nginx服务器的步骤,通过Nginx的proxy_pass指令进行接口请求的转发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

config/index 配置

  dev: {
    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      // 这里配置 '/api' 就等价于 target , 你在链接里访问 /api === http://localhost:54321
      '/api': {
        target: 'http://127.0.0.1:80/', // 真是服务器的接口地址 // http://localhost:54321/json.data.json,
        // secure: true, // 如果是 https ,需要开启这个选项
        changeOrigin: true, // 是否是跨域请求?肯定是啊,不跨域就没有必要配置这个proxyTable了.
        withCredentials:true,// 是否携带cookie
        logLevel:'debug',
        clientLogLevel: 'debug',
        pathRewrite: {
          // 这里是追加链接,比如真是接口里包含了 /api,就需要这样配置.
          '^/api': ''
        }
      }
    },
    // Various Dev Server settings
    host: '127.0.0.1', // can be overwritten by process.env.HOST
    port: 9090, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    // Use Eslint Loader?
    // If true, your code will be linted during bundling and
    // linting errors and warnings will be shown in the console.
    useEslint: false,
    // If true, eslint errors and warnings will also be shown in the error overlay
    // in the browser.
    showEslintErrorsInOverlay: false,

    /**
     * Source Maps
     */

    // https://webpack.js.org/configuration/devtool/#development
    devtool: 'cheap-module-eval-source-map',

    // If you have problems debugging vue-files in devtools,
    // set this to false - it *may* help
    // https://vue-loader.vuejs.org/en/options.html#cachebusting
    cacheBusting: true,

    cssSourceMap: true
  },

在src/axios/index.js

import axios from 'axios'

axios.create({
  // baseURL:readPath , // 设置服务器地址
  timeout: 60 * 1000, // 超时设置
  withCredentials: true, // 检查跨站点访问控制
  changeOrigin: true// 允许跨域
})


// axios.defaults.baseURL = env === 'development' ? '/api' : window.location.protocol + '//' + window.location.host; // 配置axios请求的地址
// axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
axios.defaults.crossDomain = true;
axios.defaults.withCredentials = true;  //设置cross跨域 并设置访问权限 允许跨域携带cookie信息


axios.interceptors.request.use(
  request => {
    // request.headers['access_token'] = '123456789'
    console.log('正在拦截请求。。。。。。。。。')
    console.log(request)
    return request
  },
  error => {
    console.log("报错了。。。")
    return Promise.reject(error)
  }
)
axios.interceptors.response.use(
  response => {
    console.log('响应啦。。。')
    console.log(response)
    return response
  },
  error => {
    console.log("响应异常啦。。。")
    return Promise.reject(error)
  }
)

export default axios

在main.js 中配置

import Vue from 'vue'
import App from './App'
import router from './router'
import axios from './axios'
// import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.config.productionTip = false

// axios.defaults.crossDomain = true;
// axios.defaults.withCredentials = true;
// axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
Vue.use(VueAxios, axios)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

将文件dist 上传至 nginx 文件目录下

在nginx 中confi/nginx.conf配置

   35     server {
     36         listen       80;
     37         server_name  localhost;
     38 
     39         #charset koi8-r;
     40 
     41         #access_log  logs/host.access.log  main;
     42 
     43         location / {
     44             root   dist;
     45             index  index.html ;
     46         }
     47 
     48          location /api {  #4.当请求跨域时配置端口转发
     49             proxy_pass http://192.168.31.34/; #5.转发地址注意尾部要加上/ 否则出出现请求是192.168.31.34/api/xxxx ,实际真实是 192.168.31.34/xxxx
     50         }
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值