Last active
December 19, 2019 08:58
-
-
Save Oskang09/60a757706676fa32fd2faaac7659e937 to your computer and use it in GitHub Desktop.
支付宝小程序 Code Style 样本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const App = getApp(); | |
Page({ | |
data: { | |
// data decleration 应该放上面,方便进来查看变数的时候直接看得到 | |
}, | |
async fetchStore() { | |
// 处理API 并且获取data, 和处理 error handling 之类的 | |
return data; | |
}, | |
async onShow(query) { | |
const { //处理Query 部分方便查看 } = query; | |
my.showLoading({ content: "Authorizing ..." }); | |
try { | |
await App.authUser() | |
} catch (error) { | |
// 这边处理 如果授权失败 可以用我刚加进去的 error-page | |
my.hideLoading(); | |
// 通常授权失败后 需要显示 error page 再重新授权 所有放 return 停止目前的 callstack | |
return my.alert({ content: error.message }) | |
} | |
// 我不确定需不需要 hide 了才开新的 如果需要当然就要 my.hideLoading(); | |
my.showLoading({ content: "Loading ..." }); | |
// 根据情况,如果那个 API 在其他页面也需要用到你可以放去 App-level 的变数 | |
// 如果只是这个内部页面用到 这边弄就好了 | |
const data = await this.fetchStore(); | |
const data = await App.fetchStore(); | |
// setData() 会刷新页面 所有处理完了才一次过 setData() 而且通常这类的是 异步(async)进行 | |
// 跑了 setData() 才跑其他步骤 例如 my.hideLoading(). | |
this.setData({ data }); | |
my.hideLoading(); | |
}, | |
// 事件处理 | |
async handleClick() { | |
// 一样在事件处理 我们会在这边指定 loading 的步骤 和 获取data 因为才不会有 redundant 的情况. | |
my.showLoading({ content: 'Loading ...' }) | |
const newData = await this.fetchStore(); | |
this.setData({ data: newData }) | |
my.hideLoading(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment