自己閑來無事使用uniapp編寫app,發(fā)現(xiàn)在

image.png
中有2個選項,分別是v3編譯器和fast啟動模式。
然后自己就勾選了,編譯后在chrome瀏覽器中運行沒有問題,就嘗試打包到安卓和ios下,上真機后發(fā)現(xiàn)網(wǎng)絡(luò)請求一直轉(zhuǎn)圈,自己就很詫異。
由于我項目中網(wǎng)絡(luò)請求都使用了Promise異步方式。而且所有的請求完畢都使用的finally方法。類似如下:
onClick() {
setTimeout(() => {
let that = this;
let promise, instance;
that.context = "";
promise = new Promise((resolve, reject) => {
instance = uni.request({
url: 'http://localhost:8008/api/goods-detail', //僅為示例,并非真實接口地址。
data: {
sku_id: "1"
},
header: {
"Content-Type": "application/json",
"Authorization": "",
"Accept": "application/vnd.app.v1+json",
},
method:"POST",
success: (res) => {
res.data = that.toJSON(res.data);
resolve(res);
},
fail: reject
});
});
promise.then(r => {
console.info(r)
that.context += "resolve:成功得調(diào)用";
});
promise.catch(e => {
console.info(e)
that.context += "reject:異常失敗調(diào)用"
});
promise.finally(f => {
that.context += ";finally調(diào)用"
});
}, 1000);
}
使用v3打包后在調(diào)試中發(fā)現(xiàn)會報如下錯誤信息:
[JS Framework] Failed to execute the callback function:
TypeError: promise.finally is not a function. (In 'promise.finally(function (f) {
但是不使用v3編譯則沒問題,從而推斷出v3編譯不支持該特性。所以我就在main.js文件中加入如下代碼解決該問題:
// 用于fixed 使用v3編譯不支持finally回調(diào)
if (Promise.finally === undefined) {
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => { throw reason })
);
};
}
重新編譯,打包OK
另外使用v3編譯后還有一些樣式上的變化。所以還需要一些調(diào)試和適配。
也有可能是我寫的練手項目的樣式不夠牢固把つ﹏?。