首页 >> js开发 >> jsvue实现把接口单独存放在一个文件方式js大全
jsvue实现把接口单独存放在一个文件方式js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
第一步:在src/router目录下,建立一个js文件(文件名:httpConfig.js);第二步:在httpConfig.js文件里面写上
const aa = 'http://192.168.1.123';//本地测试
const config = {
bb: aa+ '/article/articleListPage',//所需的接口
}
//需要让外部拿到
export default config;
const aa = 'http://192.168.1.123';//本地测试
const config = {
bb: aa+ '/article/articleListPage',//所需的接口
}
//需要让外部拿到
export default config;第三步:掉接口
_this.$http({
url: _this.$httpConfig.bb,
//接口地址
method: 'POST',
//请求方式
contentType: "application/json;",
//内容类型
headers: {
'Content-Type': 'application/json' //请求头
},
dataType: "json",
//数据格式
async: false,
//设置同步/异步
data: {
//所需参数
},
}).then(function(response) {
console.log(response.data);
//打印成功信息
}, function(error) {
console.log(error);
//打印错误信息
});
_this.$http({
url: _this.$httpConfig.bb,
//接口地址
method: 'POST',
//请求方式
contentType: "application/json;",
//内容类型
headers: {
'Content-Type': 'application/json' //请求头
},
dataType: "json",
//数据格式
async: false,
//设置同步/异步
data: {
//所需参数
},
}).then(function(response) {
console.log(response.data);
//打印成功信息
}, function(error) {
console.log(error);
//打印错误信息
});补充知识:Vue项目整合接口到同一个配置文件补充知识:补充知识:Vue项目整合接口到同一个配置文件首先创建一个js文件,我的命名是用api.js。
//数据接口的ip和端口
const ip = 'http://192.168.101.127:8080';
const zymllist = ip+'/hg6000/dsjjm/zymllist.do';
const wsqk = ip+'/hg6000/dsjjm/wssj.do';
const fdcqk1 = ip+'/hg6000/dsjjm/fdcqkZ.do';
const fdcqk2 = ip+'/hg6000/dsjjm/fdcqkB.do?sbno=2021-09&xzqhbm=420000&xzqhjb=2';
const sq = ip+'/hg6000/dsjjm/hbssq.do'
//一定要注册才可以使用
export default {
zymllist: zymllist,
wsqk: wsqk,
fdcqk1:fdcqk1,
fdcqk2:fdcqk2,
sq:sq,
}
//数据接口的ip和端口
const ip = 'http://192.168.101.127:8080';
const zymllist = ip+'/hg6000/dsjjm/zymllist.do';
const wsqk = ip+'/hg6000/dsjjm/wssj.do';
const fdcqk1 = ip+'/hg6000/dsjjm/fdcqkZ.do';
const fdcqk2 = ip+'/hg6000/dsjjm/fdcqkB.do?sbno=2021-09&xzqhbm=420000&xzqhjb=2';
const sq = ip+'/hg6000/dsjjm/hbssq.do'
//一定要注册才可以使用
export default {
zymllist: zymllist,
wsqk: wsqk,
fdcqk1:fdcqk1,
fdcqk2:fdcqk2,
sq:sq,
}在其他 .vue 文件内,如需调用api.js内的接口,需要先引用:
import api from "common/js/api";
import api from "common/js/api";引用完成后,使用如下:
this.axios
.get(api.sq, {
//api.sq 为 api.js 文件夹中,名称为 sq 的接口路径
params: {
//传入的参数
a:a
}
})
.then(res => { //此处要使用箭头函数,this指向才正确
console.log(res)
})
.catch(function(error) {
alert(error);
});
this.axios
.get(api.sq, {
//api.sq 为 api.js 文件夹中,名称为 sq 的接口路径
params: {
//传入的参数
a:a
}
})
.then(res => { //此处要使用箭头函数,this指向才正确
console.log(res)
})
.catch(function(error) {
alert(error);
});以上这篇vue实现把接口单独存放在一个文件方式就是小编分享给大家的全部内容了,希望能给大家一个参考。
const aa = 'http://192.168.1.123';//本地测试
const config = {
bb: aa+ '/article/articleListPage',//所需的接口
}
//需要让外部拿到
export default config;
const aa = 'http://192.168.1.123';//本地测试
const config = {
bb: aa+ '/article/articleListPage',//所需的接口
}
//需要让外部拿到
export default config;第三步:掉接口
_this.$http({
url: _this.$httpConfig.bb,
//接口地址
method: 'POST',
//请求方式
contentType: "application/json;",
//内容类型
headers: {
'Content-Type': 'application/json' //请求头
},
dataType: "json",
//数据格式
async: false,
//设置同步/异步
data: {
//所需参数
},
}).then(function(response) {
console.log(response.data);
//打印成功信息
}, function(error) {
console.log(error);
//打印错误信息
});
_this.$http({
url: _this.$httpConfig.bb,
//接口地址
method: 'POST',
//请求方式
contentType: "application/json;",
//内容类型
headers: {
'Content-Type': 'application/json' //请求头
},
dataType: "json",
//数据格式
async: false,
//设置同步/异步
data: {
//所需参数
},
}).then(function(response) {
console.log(response.data);
//打印成功信息
}, function(error) {
console.log(error);
//打印错误信息
});补充知识:Vue项目整合接口到同一个配置文件补充知识:补充知识:Vue项目整合接口到同一个配置文件首先创建一个js文件,我的命名是用api.js。
//数据接口的ip和端口
const ip = 'http://192.168.101.127:8080';
const zymllist = ip+'/hg6000/dsjjm/zymllist.do';
const wsqk = ip+'/hg6000/dsjjm/wssj.do';
const fdcqk1 = ip+'/hg6000/dsjjm/fdcqkZ.do';
const fdcqk2 = ip+'/hg6000/dsjjm/fdcqkB.do?sbno=2021-09&xzqhbm=420000&xzqhjb=2';
const sq = ip+'/hg6000/dsjjm/hbssq.do'
//一定要注册才可以使用
export default {
zymllist: zymllist,
wsqk: wsqk,
fdcqk1:fdcqk1,
fdcqk2:fdcqk2,
sq:sq,
}
//数据接口的ip和端口
const ip = 'http://192.168.101.127:8080';
const zymllist = ip+'/hg6000/dsjjm/zymllist.do';
const wsqk = ip+'/hg6000/dsjjm/wssj.do';
const fdcqk1 = ip+'/hg6000/dsjjm/fdcqkZ.do';
const fdcqk2 = ip+'/hg6000/dsjjm/fdcqkB.do?sbno=2021-09&xzqhbm=420000&xzqhjb=2';
const sq = ip+'/hg6000/dsjjm/hbssq.do'
//一定要注册才可以使用
export default {
zymllist: zymllist,
wsqk: wsqk,
fdcqk1:fdcqk1,
fdcqk2:fdcqk2,
sq:sq,
}在其他 .vue 文件内,如需调用api.js内的接口,需要先引用:
import api from "common/js/api";
import api from "common/js/api";引用完成后,使用如下:
this.axios
.get(api.sq, {
//api.sq 为 api.js 文件夹中,名称为 sq 的接口路径
params: {
//传入的参数
a:a
}
})
.then(res => { //此处要使用箭头函数,this指向才正确
console.log(res)
})
.catch(function(error) {
alert(error);
});
this.axios
.get(api.sq, {
//api.sq 为 api.js 文件夹中,名称为 sq 的接口路径
params: {
//传入的参数
a:a
}
})
.then(res => { //此处要使用箭头函数,this指向才正确
console.log(res)
})
.catch(function(error) {
alert(error);
});以上这篇vue实现把接口单独存放在一个文件方式就是小编分享给大家的全部内容了,希望能给大家一个参考。