封装QWebChannel函数
之前每次qwebchannel请求都要new一次,流程繁琐,现在做出新的方式来封装qwebchannel,并且将axios也封装进来,实现前后端独立开发(即前端通过模拟数据进行开发,开发完成后调用qt后端进行实际调试)。
- 现在全局注册axios和qwebchannel中起到通信作用的对象
import axios from 'axios'
import QWebChannel from "./assets/thirdjs/qwebchannel";
new QWebChannel(qt.webChannelTransport,function(channel){
window.qWebChannel = channel.objects.content;
});
window.axios = axios;
let axiosQWeb = require('./assets/thirdjs/axiosQWeb');
- 编辑 axiosQWeb.js 函数
const { default: axios } = require("axios");
;(function(){
var axiosQWeb = function(mode,{slot=null,signal=null,data=null,callback=()=>{return;}}){
var url = '/'+slot;
if(mode){
axios.get(url)
.then(callback)
.catch(()=>{return;})
}else{
alert(qWebChannel[slot]);
qWebChannel[signal].connect(callback);
qWebChannel[slot].call(null, data);
}
}
window.axiosQWeb = axiosQWeb;
})();
module.exports = axiosQWeb;
- 在函数中可以直接进行使用
axiosQWeb(0,{
slot:'slot_ProjectFolder_get',
signal:'singal_ProjectFolder_get',
data:'',
callback:(str) => {
var jsonStr = JSON.parse(str);
this.DBfileList = jsonStr;
this.loadDBFileVisible = true;
}
})