小程序共享数据2 - 代码实例


import { HYEventStore } from "hy-event-store" import { getPlaylistDetail } from "../services/music" const recommendStore = new HYEventStore({ state: { recommendSongInfo: {} }, actions: { fetchRecommendSongsAction(ctx) { getPlaylistDetail(3778678).then(res => { ctx.recommendSongInfo = res.playlist }) } } }) export default recommendStore

 

引入  store
import recommendStore from "../../store/recommendStore"


数据
recommendSongs: [],

 // 发起action  网络请求
 recommendStore.dispatch("fetchRecommendSongsAction")

//简单store 的  recommendSongInfo 数组
 recommendStore.onState("recommendSongInfo", this.handleRecommendSongs)


  // ====================== 从Store中获取数据 ======================
// recommendSongInfo 数组 得到的数据 更新当前的数据
  handleRecommendSongs(value) {
    if (!value.tracks) return
    this.setData({ recommendSongs: value.tracks.slice(0, 6) })
  },

//切换页面 , 取消监听
recommendStore.offState(this.handleRecommendSongs)
 

 

posted @ 2022-09-23 10:56  杨建鑫  阅读(45)  评论(0编辑  收藏  举报