Skip to content

Commit

Permalink
Merge pull request #180 from LeachZhou/master
Browse files Browse the repository at this point in the history
新增自定义组件异步生成海报使用方式
  • Loading branch information
jasondu authored Dec 17, 2019
2 parents 835ffa4 + b3a306e commit d5da65c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,27 @@ Page({
}
})
```
## 自定义组件异步生成海报

有些场景可能需要发起ajax请求后才能获取生成海报的数据,这里提供了异步生成海报的方式。

只需要引入组件中的``poster/poster.js``,如下调用就行了,与page不同的是,需要在Poster.create中加入this。

```javascript
import Poster from '../../miniprogram_dist/poster/poster';
Component({
/**
* 自定义组件异步生成海报
*/
onCreatePoster() {
// setData配置数据
this.setData({ posterConfig: {...} }, () => {
Poster.create(true, this);
});
}
})
```

## 赞赏
<img width="350" src="https://github.com/jasondu/wxa-plugin-canvas/blob/master/zan.jpg"></img>

Expand Down
14 changes: 7 additions & 7 deletions miniprogram_dist/poster/poster.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ const defaultOptions = {
selector: '#poster'
};

function Poster(options = {}) {
function Poster(options = {}, that) {
options = {
...defaultOptions,
...options,
};

const pages = getCurrentPages();
const ctx = pages[pages.length - 1];

let ctx = pages[pages.length - 1];
if (that) ctx = that
const poster = ctx.selectComponent(options.selector);
delete options.selector;

return poster;
};

Poster.create = (reset = false) => {
const poster = Poster();
Poster.create = (reset = false, that) => {
const poster = Poster({}, that);
if (!poster) {
console.error('请设置组件的id="poster"!!!');
} else {
return Poster().onCreate(reset);
return Poster({}, that).onCreate(reset);
}
}

export default Poster;
export default Poster;

0 comments on commit d5da65c

Please sign in to comment.