Giter VIP home page Giter VIP logo

wxcomputed's Introduction

wxComputed在微信小程序开发中加入了computed自动计算属性的支持,此项目主要参考了Vue.js的依赖收集逻辑,数据变化后的视图更新操作则是通过小程序内部的Page.prototype.setData原型函数来完成。

构建打包

使用rollup进行构建

  1. 安装依赖,npm install
  2. 运行打包命令,npm run build,在dist目录下生成打包文件

使用例子

将打包后的wxComputed.min.js文件放在lib目录中,在需要用到的页面中引入
或使用npm安装,npm install --production wx-computed

<!-- index.wxml -->
<view class="container">
    <view>firstName</view>
    <input bindinput="inputEvent" data-type="firstName" value="{{firstName}}" />
    <view>lastName</view>
    <input bindinput="inputEvent" data-type="lastName" value="{{lastName}}" />
    <view>fullName: {{ fullName }}</view>
</view>
// index.js
// 使用npm
import initComputed from 'wx-computed'
// 手动引入
import initComputed from '../../lib/wxComputed.min.js'

Page({
  data: {
    lastName: 'aa',
    firstName: 'bb',
  },
  inputEvent(e) {
    const {type} = e.currentTarget.dataset
    this.setData({
        [type]: e.detail.value,
    })
  },
  // 这里放入computed属性
  computed: {
    // 这是一个函数,返回值为此计算属性的值
    fullName() {
        return this.data.lastName + '-' + this.data.firstName
    },
  },
  onLoad() {
    // 在onLoad生命周期中执行computed属性初始化
    initComputed(this)
  },
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.