在小程序的页面中,可以使用onShow生命周期函数来监听小程序重新进入页面的事件。在该函数中,可以获取保存在本地的数据,并根据数据重新绘制canvas内容。

首先,在onShow生命周期函数中获取保存在本地的数据,如果存在数据,则更新data中的相关属性。

onShow() {
  const bh = wx.getStorageSync('bh');
  const mc = wx.getStorageSync('mc');
  const zt = wx.getStorageSync('zt');
  const dw = wx.getStorageSync('dw');
  const date = wx.getStorageSync('date');
  const arr1 = wx.getStorageSync('arr1');
  const arr2 = wx.getStorageSync('arr2');
  
  if (bh && mc && zt && dw && date && arr1 && arr2) {
    this.setData({
      bh,
      mc,
      zt,
      dw,
      date,
      arr1,
      arr2
    });
    this.drawTable();
  }
},

然后,在drawTable函数中,将获取到的数据更新到canvas中。

drawTable() {
  // ...

  const { bh, mc, arr1, dw, date, arr2, zt } = this.data;
  
  // ...

  const newRow = [
    { x: 60, y, width: 90, text: "G-2023-" + bh },
    { x: 150, y, width: 70, text: mc },
    { x: 220, y, width: 55, text: arr1 },
    { x: 275, y, width: 90, text: '' },
    { x: 365, y, width: 210, text: dw },
    { x: 575, y, width: 70, text: date },
    { x: 645, y, width: 65, text: '3 Kg' },
    { x: 710, y, width: 120, text: arr2 },
    { x: 830, y, width: 60, text: '' },
    { x: 890, y, width: 60, text: zt },
    { x: 950, y, width: 80, text: '' },
    { x: 1030, y, width: 65, text: '' },
    { x: 1095, y, width: 65, text: '' }
  ];

  // ...

  y += 50
  this.setData({ y });
  this.ctx.draw();
  // ...
},

这样,在重新进入小程序后,会根据保存的数据重新绘制canvas中的内容

const app = getApp Page data disabled false canvasId myCanvas clickCount 0 maxClickCount 11 tableData bh mc zt dw date arr1 arr2 array1新鲜 腐烂干燥

原文地址: https://www.cveoy.top/t/topic/ihtJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录