Key points of wechat applet development

Posted by cool-palace-ceo on Sat, 21 Dec 2019 20:40:55 +0100

  1. Citation of js
    In JS:
    //In the referenced js
    
    //Methods or properties that you want to be called by other js
    var local_data = [
        {
            name: john,
            age: 19
        },
        {
            name: mike,
            age: 29
        }
    ]
    function convertToStartsArray(starts){
        var num = starts.toString().substring(0,1);
        var array = [];
        for(var i=0;i<=5;i++){
            if(i<=num){
                array.push(1);
            }else{
                array.push(0);
            }
        }
        return array;
    }
    
    //Place the property or method to be called in it
    module.exports = {
        local_data: local_data,
        converToStartsArray: convertToStartsArray
    }

    JS files that reference other JS properties or methods:

    var utils = require("../../utils/util.js")
    
    Page({
        data: {
        
        },
        onLoad: function(event){
            var data = utils.local_data;
            var convertToStartsArray = utils.converToStartsArray;
        }
    })

     

  2. References to external wxml and wxss
    wxss uses @ import to import external style files:
    @import 'post-item/post-item-template.wxss';
    
    swiper {
      width: 100%; 
      height: 600rpx;
    }
    
    swiper image {
      width: 100%; 
      height: 600rpx;
    }

    Wxml uses < import SRC = '' / > to import external wxml files:

    <import src='post-item/post-item-template.wxml' />
    
    <view>
      <!-- Rotation chart -->
      <swiper catchtap='onSwiperTap' vertical='{{false}}' indicator-dots='true' autoplay='true'>
        <swiper-item>
          <image src='/images/wx.png' data-post-id='3'></image>
        </swiper-item>
        <swiper-item>
          <image src='/images/vr.png' data-post-id='4'></image>
        </swiper-item>
      </swiper>
    </view>

     

  3. Create and use templates
    Create a template: use the < template name = 'template name' "> < template > label most out of office, and use the" name "attribute to specify the template reference name
    //js code
    <template name='postItem'>
      <view class='post-container'>
        <view class='post-author-date'>
          <image class='post-author' src='{{avatar}}'></image>
          <text class='post-date'>{{date}}</text>
        </view>
      </view>
    </template>
    
    //Wxss code, file name is post-item-template.wxss
    .post-container {
      display: flex;
      flex-direction: column;
      margin-top: 20rpx;
      margin-bottom: 40rpx;
      background-color: #fff;
      border-bottom: 1px solid #ededed;
      border-top: 1px solid #ededed;
      padding-bottom: 5px;
    }

    Use template: use < template is = 'template name' data = '{{data |... Data}' / >
    ——”{{data}} "is the complete data input mode, and the format of the template internal reference is data.xxx;
    ——"... data" is the auto expansion data transfer mode, and the format introduced in the template is xxx;

    //js code
    <import src='post-item/post-item-template.wxml' />
    
    <view>
      <!-- list -->
      <block wx:for='{{postList}}' wox:for-item='item'>
        <!-- template Template -->
        <view catchtap='onPostTap' data-post-id='{{item.postId}}'>
          <template is="postItem" data='{{...item}}' />
        </view>
      </block>
    </view>
    
    
    //wxss code
    @import 'post-item/post-item-template.wxss';
    
    swiper {
      width: 100%; 
      height: 600rpx;
    }

     

  4.  

Topics: Attribute