微信小程式使用async/await語法
1.在微信小程式專案新增package.json檔案或者直接npm init.
2.在package.json中新增regenerator包和版本
`"devDependencies": {
"regenerator":"0.13.3"}`
3.微信開發者工具-》工具-》npm構建
4.在需要使用async/await語法的檔案引入regeneratorRuntime
const regeneratorRuntime = require('regenerator-runtime')
5.使用async/await語法
lifetimes: { attached:async function(){ // 在元件例項進入頁面節點樹時執行 let data= await req(this.properties.apiType); console.log(data) }, detached() { // 在元件例項被從頁面節點樹移除時執行 }, },
需要注意this的指向,比如生命週期如果用箭頭函式會丟失this
或者直接這樣
lifetimes: { async attached(){ // 在元件例項進入頁面節點樹時執行 let data= await req(this.properties.apiType); console.log(data) }, async detached() { // 在元件例項被從頁面節點樹移除時執行 }, },