跟老师代码一样的,为什么点击收藏没有效果呢?

来源:19-11 同步异步方法对比

Follow_the_self

2018-08-28

var postsData = require('../../../data/posts-data.js')
Page({
 data: {
 },
 onLoad: function(option) {
   var postId = option.id;
   this.data.currentPostId = postId;
   var postData = postsData.postList[postId];
   this.setData({
     post_Data: postData,
   })

   var postsCollected = wx.getStorageSync('posts_Collected')
   if (postsCollected) {
     var postCollected = postsCollected[postId]
     if (postCollected) {
       this.setData({
         collected: postCollected
       })
     }
   } else {
     var postsCollected = {}
     postsCollected[postId] = false;
     wx.setStorageSync('posts_Collected', postsCollected)
   }
 },
 onCollectionTap: function(event) {
   // this.getPostsCollectedSyc();
   this.getPostsCollectedAsy();
 },
 getPostsCollectedAsy: function() {
   var that = this;
   wx.getStorage({
     key: "posts_collected",
     success: function(res) {
       var postsCollected = res.data;
       var postCollected = postsCollected[that.data.currentPostId];
       //收藏变成未收藏,未收藏变成收藏
       postCollected = !postCollected;
       postsCollected[that.data.currentPostId] = postCollected;
       that.showToast(postsCollected, postCollected);
     }
   })
 }


写回答

1回答

慕虎9030788

2018-08-28

报什么错? 

wxml是不是没写catchtap

0
1
Follow_the_self
catchtap是写了的,调用同步没问题 一调用异步就失效
2018-08-30
共1条回复

微信小程序入门与实战(全新版) 超20000人学习的好课

4年同步微信官方迭代,累计20000+人学习, 比微信官方更火爆!

23866 学习 · 6899 问题

查看课程