从零开始写微信小程序--写一个备忘录【二】

本文介绍如何从零开始构建一个微信小程序备忘录应用。主要包括:主页面展示备忘录记录,右下角有新建按钮,底部有TabBar实现页面切换。新建笔记页面包含输入框和编辑/删除功能。在代码实现上,涉及cells组件的引入,WXML布局,JS文件读取缓存数据,以及在app.json中配置TabBar。

接着上次的文章,开始这个项目需要简单构思一下有什么页面,每个页面有些什么。这个小程序是这样的:

主页面有一条条的备忘录记录,右下角有个新建笔记的按钮,主界面下方有toBar,可以进行页面的切换。

进入新建笔记的页面后有一个输入框,上方有个Editor的按钮,点击可以隐藏或者显示。右上方有个删除的按钮,可以删除该条笔记。

第一步是首页的代码,因为是使用cells组件,我们需要引入index.json中引入组件。

{
  "usingComponents": {
        "mp-slideview": "/components/slideview/slideview",
        "mp-cells": "/components/cells/cells",
        "mp-cell": "/components/cell/cell"
  }
}

然后在wxml中使用

<!--index.wxml-->
<view class="wrapper">
 <mp-cells ext-class="my-cells" title="备忘录">
    <mp-slideview  wx:for="{{noteList}}" wx:key="{{index}}" buttons="{{slideButtons}}" bindbuttontap="slideButtonTap" data-id="{{item.id}}">
        <mp-cell data-id="{{item.id}}" bindtap="toNote" value="{{item.title}}" footer="{{item.time}}"></mp-cell>
    </mp-slideview>
</mp-cells>
  <view>
    <navigator url="/pages/note/editor" class="navigator">
    <button class="add-note">
      <image class="add-note-img" bindtap="addNote" src="/images/add-note.png"></image>
    </button>
    </navigator>
  </view>
</view>

最后是index.js文件,主要是在页面加载后读取缓存中的备忘录信息。

// pages/home/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    'noteList': []
  },

  slideButtonTap(e) {
    console.log(e.detail)
  },
  toNote(e) {
    let id = e.currentTarget.dataset['id'];
    wx.navigateTo({
      url: '/pages/note/editor?id=' + id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    /**读取缓存 */
    var that = this;
    wx.getStorage({
      key: 'noteList',
      success(res) {
        that.setData({
          "noteList": res.data
        })
      }
    })
  }
})

最后,在app.json中添加一个ToBar,作为主页和关于页的导航。

 "tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [
      {
        "pagePath": "pages/home/index",
        "iconPath": "images/tabbar/note1.png",
        "selectedIconPath": "images/tabbar/note2.png",
        "text": "笔记"
      },
      {
        "pagePath": "pages/about/index",
        "iconPath": "images/tabbar/about1.png",
        "selectedIconPath": "images/tabbar/about2.png",
        "text": "关于"
      }
    ]
  },

本篇结束。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值