const store = require('../../utils/store.js'); Component({ options: { addGlobalClass: true }, properties: { // 「添加」这一格不是每个页面都该有:计划页/记录页只是在既有宠物之间切, // 建档入口在首页和「我的」就够了,到处摆一个会让这条 chip 变得很长 showAdd: { type: Boolean, value: true }, }, data: { pets: [], currentId: 0, }, lifetimes: { attached() { this.refresh(); this._unsub = store.subscribe(() => this.refresh()); }, detached() { if (this._unsub) this._unsub(); }, }, methods: { refresh() { this.setData({ pets: store.getPets(), currentId: store.currentPetId() || 0, }); }, onPick(e) { const id = e.currentTarget.dataset.id; store.switchPet(id); this.triggerEvent('change', { id }); }, onAdd() { this.triggerEvent('add'); }, }, });