当前位置:   article > 正文

vue.config.js配置跨域 axios.create实例封装 出现跨域问题_feature flag __vue_prod_hydration_mismatch_details

feature flag __vue_prod_hydration_mismatch_details__ is not explicitly defin

vue.config.js文件一样

  1. const { defineConfig } = require( '@vue/cli-service' )
  2. const path = require( 'path' )
  3. module.exports = defineConfig( {
  4. transpileDependencies: true,
  5. productionSourceMap: false,
  6. publicPath: process.env.NODE_ENV === 'production' ? '/production-sub-path/' : '/',
  7. outputDir: 'dist',
  8. assetsDir: 'static',
  9. devServer: {
  10. port: 8090, // 修改开发服务器运行的端口号
  11. open: true,
  12. host: "localhost",
  13. proxy: {
  14. '/appApi': {
  15. target: 'https://****************/appApi', // 反向代理 线上
  16. // target: 'http://192.168.3.17:8092', // 本地
  17. ws: true,
  18. changeOrigin: true,
  19. pathRewrite: {
  20. '^/appApi': ''
  21. }
  22. }
  23. },
  24. },
  25. pluginOptions: {
  26. "style-resources-loader": {
  27. preProcessor: "less",
  28. patterns: [path.resolve( __dirname, "./src/assets/style/commons.less" )] // 全局引入less
  29. }
  30. },
  31. chainWebpack: ( config ) => {
  32. config.plugin( 'define' ).tap( ( definitions ) => {
  33. Object.assign( definitions[0], {
  34. __VUE_OPTIONS_API__: 'true',
  35. __VUE_PROD_DEVTOOLS__: 'false',
  36. __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false'
  37. } )
  38. return definitions
  39. } )
  40. }
  41. } )

两种情况,第一种是使用了axios.create实例封装后的请求,第二种是 直接使用axios进行请求

  1. require.js文件
  2. import axios from "axios";
  3. import router from "../router";
  4. import { ElMessage } from 'element-plus';
  5. import { info } from '@/store/index';
  6. // 实例
  7. const useInfo = info()
  8. /** 默认配置*/
  9. const instance = axios.create( {
  10. baseURL: window._CONFIG["domianURL"],
  11. timeout: "10000",
  12. headers: {
  13. "Content-Type": "application/json;charset=UTF-8",
  14. "Access-Control-Allow-Origin": "*",
  15. },
  16. } );
  17. /** 请求拦截 */
  18. instance.interceptors.request.use( ( config ) => {
  19. // 登录验证
  20. if ( useInfo.token ) {
  21. config.headers.Authorization = `Bearer ${ useInfo.token }`;
  22. }
  23. return config;
  24. } );
  25. /** 响应拦截 */
  26. instance.interceptors.response.use( ( response ) => {
  27. if ( response && response.data.status == 200 ) {
  28. return response.data;
  29. } else {
  30. if ( response.status == 501 ) {
  31. router.push( "/login" );
  32. ElMessage.error( '信息丢失,请重新登录' );
  33. } else {
  34. ElMessage.error( response.data.msg );
  35. console.log( response.data, 'data' )
  36. return Promise.reject( response.data );
  37. }
  38. }
  39. }, ( error ) => {
  40. if ( error.message == 'timeout of ' + error.config.timeout + 'ms exceeded' ) {
  41. ElMessage.error( '请求时间超过' + ( error.config.timeout / 1000 ) + '秒,请保证网络通畅' );
  42. } else {
  43. ElMessage.error( error );
  44. }
  45. } );
  46. /**封装常用请求类型 */
  47. export const post = ( url, data ) => instance.post( url, data );
  48. export const get = ( url, params ) => instance.get( url, { params } );
  49. export default instance
  1. // login.js文件
  2. import { post } from "@/utils/request";
  3. // 登录
  4. export const userLogin = ( data ) => post( '/appApi/app/userLogin/userLogin', data );

 

  1. import { ref } from "vue";
  2. import { ElMessage } from 'element-plus'
  3. import { User, Lock } from '@element-plus/icons-vue'
  4. import { info } from '@/store/index'
  5. import { useRouter } from "vue-router";
  6. /**变量 */
  7. const ruleForm = ref( {
  8. mobile: "",
  9. password: "",
  10. flag: 2 // 1小程序 2后台
  11. } );
  12. const rules = {
  13. mobile: [
  14. {
  15. required: true,
  16. message: "请输入用户名",
  17. trigger: "blur",
  18. },
  19. ],
  20. password: [
  21. {
  22. required: true,
  23. message: "请输入密码",
  24. trigger: "blur",
  25. },
  26. ],
  27. };
  28. /**实例 */
  29. const useInfo = info()
  30. const router = useRouter()
  31. // 这种不行 一直显示跨域 似乎没有被代理到,这种是经过axios.create封装后的
  32. import { userLogin } from "@/api/login.js"
  33. userLogin( '/appApi/app/userLogin/userLogin', ruleForm.value )
  34. // 这种可以跨域,这种直接使用axios
  35. import axios from 'axios'
  36. axios.post( '/appApi/app/userLogin/userLogin', ruleForm.value )

------------------------------------------------第一种 一直显示跨域--------------------------------------------------

  1. // 这种不行 一直显示跨域 似乎没有被代理到,这种是经过axios.create封装后的
  2. import { userLogin } from "@/api/login.js"
  3. userLogin( '/appApi/app/userLogin/userLogin', ruleForm.value )

------------------------------------------------第二种 可以正常请求--------------------------------------------------

  1. // 这种可以跨域,这种直接使用axios
  2. import axios from 'axios'
  3. axios.post( '/appApi/app/userLogin/userLogin', ruleForm.value )

有前辈遇到过这种问题吗?axios.create怎么跨域,是我配置的问题吗?请大佬 指导一下

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号