taro
taro copied to clipboard
[H5]自定义组件 createCanvasContext 报错
问题描述 H5 createCanvasContext 报错
复现步骤 [复现问题的步骤] 自定义组件中使用Canvas 且 canvasId 通过 props 传入
// 组件
export default class CanvasTest extends Component {
static options = {
addGlobalClass: true
}
componentWillMount() {
const { lazyLoad } = this.props;
if (!lazyLoad) {
this.init();
}
}
init = () => {
const { canvasId, onInit } = this.props;
const ctx = Taro.createCanvasContext(canvasId, this);
const query = Taro.createSelectorQuery().in(this.$scope);
query.select(`#${canvasId}`).boundingClientRect(res => {
if (onInit) {
onInit(ctx, res.width, res.height);
}
}).exec();
}
render() {
const { canvasId, className } = this.props
return (
<Canvas
className={classNames('charts', className)}
id={canvasId}
canvasId={canvasId}
/>
)
}
}
// 使用
<CanvasTest canvasId='test-id' onInit={this.initCanvas} />
期望行为 createCanvasContext正常返回
报错信息


系统信息
- 操作系统:macOS 10.14.6
- Taro 版本 v1.3.15
- Node.js 版本 10.12.0
- 报错平台 [h5]
补充信息 H5转换时canvasId 计算错误
编译后组件render 方法
render() {
const { canvasId, className } = this.props;
return <Canvas className={classNames('charts', className)} id={canvasId} canvasId={canvasId} onTouchStart={this.touchStart} onTouchMove={this.touchMove} onTouchEnd={this.touchEnd} onLongPress={this.longPress} ref={ref => {
this['__taroref_canvasId'] = ref; // 这里实际取到的是参数名称,而不是参数值,正确应该是 “__taroref_test-id”
}} />;
}
欢迎提交 Issue~
如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏
如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。
Good luck and happy coding~
CC @Littly
Duplicate of #4337