Skip to content

Commit 8699788

Browse files
author
xuying.xu
committed
feat: 增加小程序onError
1 parent 50235fa commit 8699788

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

packages/my/src/index.tsx

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Component({
2121
width: null,
2222
height: null,
2323
type: '2d', // canvas 2d, 基础库 2.7 以上支持
24+
onError: (error) => {},
2425
},
2526
/**
2627
* 组件创建时触发
@@ -35,27 +36,33 @@ Component({
3536
return;
3637
}
3738
const { id } = this.data;
38-
const query = my.createSelectorQuery({ page: this.$page });
39-
query
40-
.select(`#${id}`)
41-
.boundingClientRect()
42-
.exec((res) => {
43-
// 获取画布实际宽高, 用props的宽高做失败兜底
44-
const { width, height } = res && res[0] ? res[0] : this.props;
45-
const pixelRatio = getPixelRatio();
46-
// 高清解决方案
47-
this.setData(
48-
{
49-
width: width * pixelRatio,
50-
height: height * pixelRatio,
51-
},
52-
() => {
53-
const myCtx = my.createCanvasContext(id);
54-
const context = F2Context(myCtx);
55-
this.canvasRender({ width, height, context, pixelRatio });
56-
}
57-
);
58-
});
39+
const { onError } = this.props;
40+
try {
41+
const query = my.createSelectorQuery({ page: this.$page });
42+
query
43+
.select(`#${id}`)
44+
.boundingClientRect()
45+
.exec((res) => {
46+
// 获取画布实际宽高, 用props的宽高做失败兜底
47+
const { width, height } = res && res[0] ? res[0] : this.props;
48+
const pixelRatio = getPixelRatio();
49+
// 高清解决方案
50+
this.setData(
51+
{
52+
width: width * pixelRatio,
53+
height: height * pixelRatio,
54+
},
55+
() => {
56+
const myCtx = my.createCanvasContext(id);
57+
const context = F2Context(myCtx);
58+
this.canvasRender({ width, height, context, pixelRatio });
59+
}
60+
);
61+
});
62+
} catch (error) {
63+
onError && onError(error);
64+
throw error;
65+
}
5966
},
6067
didUpdate() {
6168
const { canvas, props } = this;
@@ -78,23 +85,29 @@ Component({
7885
},
7986
onCanvasReady() {
8087
const { id } = this.data;
81-
const query = my.createSelectorQuery();
82-
query
83-
.select(`#${id}`)
84-
// @ts-ignore
85-
.node()
86-
.exec((res) => {
87-
if (!res[0]) {
88-
return;
89-
}
90-
const canvas = res[0].node;
91-
const { width, height } = canvas;
92-
const pixelRatio = getPixelRatio();
93-
canvas.width = width * pixelRatio;
94-
canvas.height = height * pixelRatio;
95-
const context = canvas.getContext('2d');
96-
this.canvasRender({ width, height, pixelRatio, context });
97-
});
88+
const { onError } = this.props;
89+
try {
90+
const query = my.createSelectorQuery();
91+
query
92+
.select(`#${id}`)
93+
// @ts-ignore
94+
.node()
95+
.exec((res) => {
96+
if (!res[0]) {
97+
return;
98+
}
99+
const canvas = res[0].node;
100+
const { width, height } = canvas;
101+
const pixelRatio = getPixelRatio();
102+
canvas.width = width * pixelRatio;
103+
canvas.height = height * pixelRatio;
104+
const context = canvas.getContext('2d');
105+
this.canvasRender({ width, height, pixelRatio, context });
106+
});
107+
} catch (error) {
108+
onError && onError(error);
109+
throw error;
110+
}
98111
},
99112
canvasRender({ width, height, pixelRatio, context }) {
100113
if (!width || !height) {

0 commit comments

Comments
 (0)