vue跨域

一、

假如目标服务器端口是8080,我需要把vue的端口改成其他的比如8081。

在config目录下打开index.js,把port的8080改成8081
port: 8081
数据请求,后台不允许跨域,所有需要修改配置
在在config目录下打开index.js,添加以下代码
dev: {
  // Various Dev Server settings
  host: 'localhost', // can be overwritten by process.env.HOST
  port: 8081, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
  errorOverlay: true,
  notifyOnErrors: true,
  poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
  autoOpenBrowser: true,
  assetsSubDirectory: 'static',
  assetsPublicPath: '/',
  proxyTable: {
    '/api':{
      target:'http://localhost:8080',
      changeOrigin:true,
      pathRewrite:{
        '^/api':'/api'
      }
    }
  },

接着去build文件夹,打开webpack.dev.conf.js在devserver下添加
proxy: {
  '*': {//*表示所有url
    target: 'http://localhost:8080',
    changeOrigin: true,
    secure: false,
    ws: false
  },
  '/static/*': {
    target: 'http://localhost:8080',
    changeOrigin: true,
    secure: false
  }
}
最后重启服务


完成以上两步开始项目
目录结构
build
config
src
    ---api//存放url地址
    ---base//存放基础组件
    ---common//存放公共样式,图片,js
        ---img
        ---css
        ---js//尽量不用,但是有些必须不得不操作dom的时候添加js
    ---components//存放组件
    ---router//存放路由
    ---store//存放vuex
    ---util//存放封装的cookice
    ---App.vue
    ---main.js
package.json
index.html


在main.js放了一个修改wds一直请求的bug
if (module.hot) {//解决wds一直请求的bug
  module.hot.accept();
}
main.js主要代码
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview';
import 'iview/dist/styles/iview.css';
import store from './store';
import axios from 'axios';
import 'common/css/base.css';
import 'common/css/animate.css';
import jquery from 'jquery';
Vue.use(iView);
Vue.config.productionTip = false;
Vue.prototype.$http=axios;
Vue.prototype.$=jquery;
if (module.hot) {//解决wds一直请求的bug
  module.hot.accept();
}
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
项目被要求用四层路由,这一点我是拒绝的,四层路由,马拉个币,其他的用vuex不就好了,奈何我是小前端
路由这块需要判断登录类似于后台的登录拦截
router.beforeEach((to, from, next) => {
  //to即将进入的目标路由对象,from当前导航正要离开的路由, next  :  下一步执行的函数钩子
  if(to.path === '/login'||to.path === '/register'){
    next()
  }  // 如果即将进入登录路由或者进入注册,则直接放行
  else {     //进入的不是登录路由
    if(util.getCookie('userInfo')){
      //如果需要登录验证,或者已经登录成功,则直接放行
      if(islogin.getislogin()){


      }
      next()
    }    else{
      //下 一跳路由需要登录验证,并且还未登录,则路由定向到  登录路由
      next({
        path: '/login'
      })
    }
  }

})

二、使用vue-jsonp

1.npm install vue-jsonp

2.在main.js中设置:

import VueJsonp from 'vue-jsonp'

Vue.use(VueJsonp );

3.在组件中引用:

this.$jsonp(url,paramet).then(json=>{

}).catch(err=>{

});

参考网站:点击打开链接

三、使用vueResource提供的this.$http方法

this.$http({

method:'get',

url:'',

...(options)

}).then(()=>{

//成功

},()=>{

//失败

})

发送请求时的options选项对象包含以下属性:

参数类型描述
urlstring请求的URL
methodstring请求的HTTP方法,例如:'GET', 'POST'或其他HTTP方法
bodyObject,FormDatastringrequest body
paramsObject请求的URL参数对象
headersObjectrequest header
timeoutnumber单位为毫秒的请求超时时间 (0 表示无超时时间)
beforefunction(request)请求发送前的处理函数,类似于jQuery的beforeSend函数
progressfunction(event)ProgressEvent回调处理函数
credientialsboolean表示跨域请求时是否需要使用凭证
emulateHTTPboolean发送PUT, PATCH, DELETE请求时以HTTP POST的方式发送,并设置请求头的X-HTTP-Method-Override
emulateJSONboolean将request body以application/x-www-form-urlencoded content type发送

参考网站:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值