小程序学习笔记

wxs

// wxs/my-wxs.wxs
var getMax = function(array) {
  var max = undefined;
  for (var i = 0; i < array.length; ++i) {
    max = max === undefined ?
      array[i] :
      (max >= array[i] ? max : array[i]);
  }
  return max;
}

module.exports = {
  getMax: getMax
}

wxml引入

// pages/index.wxml
// wxs src 为绝对路径
<wxs src="../../wxs/my-wxs.wxs" module="info" />
<view>{{info.getMax(array)}}</view>

template

<!--wxml-->
<template name="staffName">
  <view>
    FirstName: {{firstName}}, LastName: {{lastName}}
  </view>
</template>

<template is="staffName" data="{{...staffA}}"></template>
<template is="staffName" data="{{...staffB}}"></template>
<template is="staffName" data="{{...staffC}}"></template>

page.js

Page({
  data: {
    staffA: {firstName: 'Hulk', lastName: 'Hu'},
    staffB: {firstName: 'Shang', lastName: 'You'},
    staffC: {firstName: 'Gideon', lastName: 'Lin'}
  }
})

component

// custom-component.wxml

<view>我是custom-component组件</view>

在page.json 中注册

// page.json
{
  "navigationBarTitleText": "我是title",
  "usingComponents": {
    "custom-component": "../components/custom-component"
  }
}

在page.wxml中使用组件

<custom-component/>

事件传值

// page.wxml
<button class="add-btn" bind:tap="addCounter" data-addnum ="{{num}}">+</button>
<button class="sub-btn" bind:tap="subCounter" data-subnum ="{{num}}">-</button>
<view class="custome-counter">counter:{{counter}}</view>
// page.js
 data: {
    counter:0,
    num:100
  
  }
// tap事件
addCounter(event) {
  console.log(event);
  this.setData({
    counter: this.data.counter + event.target.dataset.addnum
  })

},
subCounter(event) {
  console.log(event);
  this.setData({
    counter: this.data.counter - event.target.dataset.subnum
  })
}
原文地址:https://www.cnblogs.com/bingziweb/p/13953976.html