小程序共享数据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)
本文来自博客园,作者:杨建鑫,转载请注明原文链接:https://www.cnblogs.com/qd-lbxx/p/16721965.html