添加异常捕获

This commit is contained in:
藤之内
2019-02-15 22:41:37 +08:00
parent 99affba8f0
commit 41088c292e
2 changed files with 71 additions and 50 deletions

View File

@@ -41,9 +41,9 @@
//代码写在onload里防止阻塞页面渲染
window.onload = function () {
Rap.config({
debug: false,
debug: true,
default_page: "test/default",
app_version: '11',
app_version: '12',
comp_version: {'test_page6': 1},
css: [
'bootstrap.3.3.7.min.css'
@@ -51,7 +51,9 @@
script: [
"vue.2.5.13.js",
"vue-role.0.1.0.js"
]
],onPageLoadError:function (page,e) {
console.log('page'+page+e);
}
}).ready(function () {
if (!Rap.debug) {
//这里可以异步加载编译后的文件
@@ -61,7 +63,6 @@
//这里也可以预加载模块
return Rap.loadMod(['test/page3']); //loadMod 返回的也是promise
}).then(function () {
window.APP = Rap.app({
el: '#app'
});

View File

@@ -25,7 +25,7 @@
if (data && typeof data.then == "function") {
//返回结果是 promise//需要等待
data.then(function (res) {
next.resolve(res);
next.resolve(res);
}).catch(function (e) {
next.reject(e);
});
@@ -33,12 +33,15 @@
next.resolve(data);
}
} catch (e) {
next.reject(e);
next.reject(e);
}
};
return {
resolve: function (res) {
if(result==2){
return this;
}
success_arg = res;
result = 1;
if (success) {
@@ -49,17 +52,18 @@
reject: function (res) {
error_arg = res;
result = 2;
var reject_ok=false;
if (error) {
error(error_arg);
} else if (next) {
reject_ok=error(error_arg);
}
if (!reject_ok&&next) {
next.reject(res);
}
return this;
},
then: function (call, e) {
then: function (call) {
next = P();
success = call;
error = e;
if (result == 1) {
nextCall();
} else if (result == 2 ) {
@@ -73,10 +77,16 @@
return next;
},
catch: function (e) {
next = P();
this.then(function () {
next.resolve();
});
error = e;
if (result == 2) {
error(error_arg);
}
next.parent = this;
return next;
}
};
}
@@ -192,7 +202,12 @@
if (Rap.debug) {
script += "//@ sourceURL=" + modUrl;
}
eval(script);
try {
eval(script);
}catch (e){
throw e;
}
return p;
}
@@ -246,6 +261,9 @@
var RapReady = null;
var onPageLoadError=function () {
};
var Rap = {
config: function (config) {
@@ -267,6 +285,9 @@
}
var loadCss = Rap.loadCss(config.css);
var loadScript = Rap.loadScript(config.script);
if(config.onPageLoadError){
onPageLoadError=config.onPageLoadError;
}
Rap.promise(function () {
return loadCss;
}).then(function () {
@@ -676,6 +697,12 @@
} else {
this.loadMod(Rap.global_router.page).then(function () {
Rap.onViewChangeCallBack(modName);
}).catch(function (e) {
console.log(Rap.global_router.page+'页面加载错误');
console.log(e);
if(onPageLoadError){
onPageLoadError(Rap.global_router.page,e);
}
});
}
},
@@ -729,10 +756,7 @@
try {
content = Cache.get(key_content);
var json = JSON.parse(content);
evalScript(modUrl + ".js", url, modName, json).then(function () {
p.resolve();
});
return p;
return evalScript(modUrl + ".js", url, modName, json);
} catch (e) {
Cache.remove(key_content);
}
@@ -741,12 +765,12 @@
}
}
}
rapGet(modUrl + '.html?version=' + version).then(function (content) {
var template = "";
var style = "";
var script = "";
return rapGet(modUrl + '.html?version=' + version).then(function (content) {
var el = document.createElement('div');
el.innerHTML = content;
var style = "";
var template = "";
var script = "";
var links = [];
for (var i = 0; i < el.children.length; i++) {
var child = el.children[i];
@@ -771,39 +795,35 @@
})
})(i);
}
promise.then(function () {
var compile = Vue.compile(template);
var lc = {
render: compile.render,
staticRenderFns: compile.staticRenderFns,
style: style,
script: script,
name: key_content,
base: url
};
Cache.set(key, version);
var lc_json = {
render: compile.render.toString(),
staticRenderFns: compile.staticRenderFns.toString(),
style: style,
script: script,
name: key_content,
base: url
};
Cache.set(key_content, JSON.stringify(lc_json));
return lc;
}).then(function (lc) {
return evalScript(modUrl + ".js", url, modName, lc);
}).then(function () {
p.resolve();
});
}).catch(function () {
console.log('url:' + modUrl + " get error")
return promise;
}).then(function () {
var compile = Vue.compile(template);
var lc = {
render: compile.render,
staticRenderFns: compile.staticRenderFns,
style: style,
script: script,
name: key_content,
base: url
};
Cache.set(key, version);
var lc_json = {
render: compile.render.toString(),
staticRenderFns: compile.staticRenderFns.toString(),
style: style,
script: script,
name: key_content,
base: url
};
Cache.set(key_content, JSON.stringify(lc_json));
return lc;
}).then(function (lc) {
return evalScript(modUrl + ".js", url, modName, lc);
}).catch(function (e) {
console.log('url:' + modUrl + " get error");
console.log(e);
});
}
return p;
},
$query: function () {
var hash = window.location.hash;