美文网首页
vue简单接口封装 跨域问题处理

vue简单接口封装 跨域问题处理

作者: 一叶孤舟1990 | 来源:发表于2020-01-08 10:59 被阅读0次

vue接口封装:

第一步:解决跨域

接口请求,一般都会碰到跨域问题,在vue项目中,我们采用页面代理的方法解决跨域问题:

文件config——index.js文件

index.js

use strict'

// Template version: 1.2.7

// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path');

module.exports = {

  dev: {

    // Paths

    assetsSubDirectory: 'static',

    assetsPublicPath: '/',

      proxyTable: {

          '/api': {

            target:'https://www.XXX.com/jcrh', // 请求的第三方接口  或 后端,线上接口

            // target:'http://localhost:8081/api', //      本地

            changeOrigin:true, // 在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题

            secure: true,  //true是https    false是http

            pathRewrite:{  // 路径重写,

              '^/api': ''  // 替换target中的请求地址,也就是说以后你在请求http://api.douban.com/v2/XXXXX这个地址的时候直接写成/api即可。

            }

          },

          //物联网气泡接口代理

        '/web': {

            target: 'http://api.XXX.com/rest',//微后端服务地址

            changeOrigin: true,

            pathRewrite: {

                '^/web': ''

            }

          }

      },

    // Various Dev Server settings

    host: '0.0.0.0', // can be overwritten by process.env.HOST

    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined

    autoOpenBrowser: false,  //是否默认,启动项目自动打开页面 true自动打开  false手动

    errorOverlay: true,

    notifyOnErrors: true,

    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

    /**

    * Source Maps

    */

    // https://webpack.js.org/configuration/devtool/#development

    devtool: 'eval-source-map',

    cacheBusting: true,

    cssSourceMap: false,

  },

  build: {

    // Template for index.html

    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths

    assetsRoot: path.resolve(__dirname, '../dist'),

    assetsSubDirectory: 'static',

    assetsPublicPath: './',

    /**

    * Source Maps

    */

    productionSourceMap: false,

    devtool: '#source-map',

    productionGzip: false,

    productionGzipExtensions: ['js', 'css'],

    bundleAnalyzerReport: process.env.npm_config_report

  }

}

第二部:接口封装

我们约定俗成一般会在src文件下建立一个api文件

这个api文件,就相当于一个管理接口的仓库

api——index.js

第三部:接口运用

index.vue

<template>

  <div class="container"></div>

</template>

<script>

import { list } from '../../../src/api';

export default {

  data() {

    return {

        value1: 0,

        }

  },

  created() {

    this.test()

  },

  methods: {

//接口测试

    test(){

      var param = {"areaCodeParent":"331081000000"}

      list(param).then(res => {

        console.log(res)

      })

    }

  },

}

</script>

相关文章

  • vue简单接口封装 跨域问题处理

    vue接口封装: 第一步:解决跨域 接口请求,一般都会碰到跨域问题,在vue项目中,我们采用页面代理的方法解决跨域...

  • Vue 项目解决跨域问题

    vue 项目中解决接口跨域的方法 1.简单粗暴直接用jquery 的jsonp 来调用跨域跨域接口 2.如果本地引...

  • uni-app及vue浏览器跨域问题解决

    以猫眼电影接口为例: 假设请求接口 遇到跨域问题 vue解决跨域 在项目根目录下新建vue.config.js文件...

  • axios跨域和配置proxyTable

    使用vue-axios和vue-resource解决vue中调用网易云接口跨域的问题 vue.js学习之 跨域请求...

  • 处理接口跨域问题(vue)

    前后端分离WEB项目本地开发的跨域问题 vue-cli里有代理设置。但是!!!怕麻烦的话,啥都别说了。不如试试这个...

  • Vue学习笔记(一)

    跨域问题 vue前端跨域问题 1. 利用vue-cli框架与axios结合,访问服务器后端接口,axios不需要太...

  • Vue面试归纳

    1. Vue项目axios跨域 跨域问题出现,使用webpack-dev-server的proxy功能处理 1...

  • 用nginx的反向代理机制解决前端跨域问题

    用nginx的反向代理机制解决前端跨域问题 Vue做前台,后台走接口就会遇到跨域问题。这里Nginx做反向代理是一...

  • vue使用过程中的一些问题解决

    1.接口跨域问题 本地开发项目请求服务器接口的时候,因为客户端的同源策略,导致了跨域的问题,解决方案如下vue.c...

  • ionic1 常见问题

    video 标签scr跨域 一个访问接口service的简单封装 在controller里的调用

网友评论

      本文标题:vue简单接口封装 跨域问题处理

      本文链接:https://www.haomeiwen.com/subject/djgjactx.html