Git Product home page Git Product logo

vue_axios_spa's Introduction

vue-cli

官方脚手架

Build Setup

# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for test env with minification
npm run test 

# build for uat env with minification
npm run uat 

# build for production with minification
npm run build

# build for production and view the bundle analyzer report
npm run build --report
## 前后端数据交互之[axios](https://www.axios.com/)
#### 1.组件中调用

//从service的 getDta.js中引入你要调用的函数 import { getOrderList } from '../../service/getData';

export default { created(){//vue实例被创建后执行 this.getMsg(); //this指向vue实例 }, //该组件中所有执行函数均存放在这里 methods() { getMsg() { getOrderList({ params:params, //传参 }).then((data) =>{ //响应的结果 })
}, } }


#### 2.service/getData.js
import gitInfo from '../config/axios';    //引入封装好的axios组件
import { url } from '../config/env';      //引入接口baseUrl
export const getOrderList = (params) => getInfo(url+'具体接口路径',params,method);
//参数解释:
//params:组件中调用该方法传的参数
//url:接口路径
//method:请求方法
#### 3.axios.js

import axios from 'axios'; import router from '../router/router'

//设置全局axios默认值 axios.defaults.timeout = 15000; //15s的超时验证 axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

// 创建axios实例 const fetch = axios.create();

const getInfo = (url='',data={},type='get') =>{ if(type === 'get'){ return fetch.get(url,data).then(function (resp) { if (resp.data.data && resp.data.data.accessToken) { //更新accessToken window.localStorage.setItem('accessToken', resp.data.data.accessToken); } return resp.data ; });
}

if (type === 'post') { 
        return fetch.post(url,data).then(function(resp) {
                if (resp.data.data && resp.data.data.accessToken) { //更新accessToken
                        window.localStorage.setItem('accessToken', resp.data.data.accessToken);
                }
                return resp.data;
        })
}

};

export { getInfo }

## UI
#### [UI库-vux](https://vux.li/#/)
## 公共函数 util.js
###### 获取url中的参数

export const getQuery = (key) => { const location = window.location; const query = {}; const params = location.href.split('?')[1] || ''; if (params) { params.split('&').forEach((item) => { const queryPair = item.split('='); query[queryPair[0]] = queryPair[1]; }); }

const rst = query[key];
//解码一个编码的 URI。
return rst ? window.decodeURIComponent(query[key]) : '';

}


### 移动端调试

// App.vue export default { created() { if(process.env.NODE_ENV === 'test') { //测试环境启用 let VConsole = require('vconsole'); this.vConsole = new VConsole(); } } }

vue_axios_spa's People

Contributors

huyongyong avatar huyongyong1992 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.