search
HomeWeb Front-endJS TutorialQuick knowledge about WeChat mini program
Quick knowledge about WeChat mini programFeb 28, 2018 pm 02:47 PM
AppletsKnowledgequick check

This article mainly shares with you the quick knowledge of WeChat mini programs, which can make it easier for everyone to develop WeChat mini programs. I hope this article can help everyone.

1. Configuration

Full configuration analysis

  • ##project.config.json (project configuration file)

{
    // 文件描述
    "description": "项目配置文件",
    // 项目代码配置
    "setting": {
        // 是否检查 url 域名安全性和 TLS 版本
        "urlCheck": false,
        // 是否将项目的 es5 代码转成 es6
        "es6": true,
        // 是否自动补全 css 兼容前缀
        "postcss": true,
        // 是否压缩代码
        "minified": true,
        // 是否启用新功能
        "newFeature": true
    },
    // 编译方式
    "compileType": "miniprogram",
    // 版本号
    "libVersion": "1.7.2",
    // appid
    "appid": "touristappid",
    // 项目名
    "projectname": "haiyangbg",
    // 项目配置
    "condition": {
        // 搜索关键字
        "search": {
            "current": -1,
            "list": []
        },
        // 客服
        "conversation": {
            "current": -1,
            "list": []
        },
        // 编译方式
        "miniprogram": {
            "current": -1,
            "list": []
        }
    }
}
  • app.json (mini-program configuration)

{
  // 项目路由设置(第一项为首页)
  "pages":[
    "pages/index/index",
    "pages/logs/logs"
  ],
  // 窗口设置
  "window":{
    // 顶部导航栏背景色,必须是十六进制颜色值,如"#000000"
    "navigationBarBackgroundColor": "#9ef468",
    // 顶部导航栏显示文字
    "navigationBarTitleText": "组件展示",
    // 导航栏文字颜色,仅支持 black/white
    "navigationBarTextStyle": "black",
    // 下拉背景的文字样式,仅支持 dark/light
    "backgroundTextStyle":"light",
    // 下拉背景色,必须是十六进制颜色值,如"#000000"
    "backgroundColor": "#333",
    // 是否开启下拉刷新
    "enablePullDownRefresh": true,
    // 距离最底端触多少距离时触发触底事件,单位px
    "onReachBottomDistance": 40
  },
  //网络请求过期时间,单位毫秒
  "networkTimeout": {
    // 普通ajax请求
    "request": 20000,
    // Socket请求
    "connectSocket": 20000,
    // 文件上传
    "uploadFile": 20000,
    // 文件下载
    "downloadFile": 20000
  },
  // tab导航栏
  "tabBar": {
    // 文字的颜色
    "color": "#999",
    // 选中时文字的颜色
    "selectedColor": "#000",
    // 背景色
    "backgroundColor": "#fff",
    // 上边框的颜色,仅支持 black/white
    "borderStyle": "black",
    // tab导航栏显示在底部还是顶部(顶部不显示图片)
    "position": "bottom",
    // 导航栏列表项
    "list": [{
      // 导航到的页面路径
      "pagePath": "pages/index/index",
      // tab按钮上的文字
      "text": "组件",
      // 图片路径
      "iconPath": "img/com-l.png",
      // 选中后显示的图片
      "selectedIconPath": "img/com-d.png"
    },{
      "pagePath": "pages/logs/logs",
      "text": "API",
      "iconPath": "img/api-l.png",
      "selectedIconPath": "img/api-d.png"
    }]
  },
  // 调试信息
  "debug": true
}
  • page.json (single Page configuration)

The

json of a single page is the window field of app.json, when loaded into this page When #onLaunch ------ Triggered when the mini program initialization is completed (will only be called once)

onShow ------ 1. Mini program starts 2. Enter the foreground display from the background, trigger

  • onHide ------ When the applet enters the background from the foreground, trigger

  • onError -- ---- 1. A script error occurred 2. The api call failed and was triggered with an error message

  • Single page Page life cycle

  • onLoad ------ Triggered when the page is loaded (will only be called once)

onShow ------ When the page is displayed, Trigger
  • onReady ------ When the initial rendering is completed, trigger (will only be called once)

  • onHide ----- - When the page is hidden, trigger

  • onUnload ------ When the page is unloaded, trigger

  • Details Life cycle classification:
  • 1. Mini program launch:
  • App.onLaunch

    - ->
  • App.onShow
- -> ; Register pages in app.json pages (in index order) - -> Set app routing as homepage routing - -> Deep copy homepage page parameters - -> Initialize homepage data - ->
Page. onLoad

- -> Page.onShow - ->

Page.onReady

2. Switch to the background (app and page life cycles overlap ): The applet was switched to the background - -> page.onHide - -> App.onHide - -> Switched back to the applet Program - -> App.onShow - ->

page.onShow

3. Jump page:

old represents the previous page , new means new pagenavigateToJump- -> Set the route as the target page route- -> old.onHide - -> Initialization page data - ->

new.onLoad

- ->

new.onShow

- ->

new.onReady
  1. redirectToRedirect- -> Set routing- -> old.onUnload - -> init data - -> new .onLoad - -> new.onShow - ->

    new.onReady
  2. ##navigateBack Page return - -> Set routing - -> old.onUnload - -> init data - -> new.onShow

    ##reLaunch
  3. Restart- -> Set routing- ->
  4. old.onUnload

    - -> init data - -> new.onLoad - -> new.onShow - -> new.onReady

  5. ##switchTabTab switch (picture taken from Official document)3. Data binding and rendering

  6. Data Binding: { { message } }

  7. Rendering

Quick knowledge about WeChat mini program##List rendering:

- wx:for=" { { message } } "  
- wx:for-index="idx" (设置索引的变量名,默认 index ) 
- wx:for-item="itemName"(设置每一项的变量名,默认item )

Rendering block:
  1. Conditional rendering:
  2. - wx:if="boolean" - wx:elif="boolean" -wx:else="boolean" == (if - else if - if)
    - hidden="boolean"
  3. 4. Template )
  • Basic usage:

  • (Define code snippets, which can be called in different places, use the name attribute as the name of the template, and use the is attribute when calling)

    // 源码(需要和调用的页面在同一个wxml里)
    <template>
      <view>
        I am {{ name }} 
      </view>
    </template>
    
    // 调用
    <template></template>
    
    // js 数据
    Page({
      data: {
        message: {
          name: '海洋饼干'
        }
      }
    })
  • Advanced usage
  • When many pages needed the same template, the template needed to be imported

First Create a new

template folder

in the pages folder, and create a new template.wxml file in the folder

// template.wxml
<template>
  <view>
    I am {{ name }} 
  </view>
</template>
// page.wxml 调用
<import></import>
<template></template>

// js 数据
Page({
  data: {
    message: {
      name: '海洋饼干'
    }
  }
})

5. Event

Event list

Click Event

Click eventtap

Long press event (more than 0.35 seconds)
    longpress
  1. ##Touch
    event
  • touch start
  • touchstart
  • Start moving after touching
  • touchmove
  • Interrupted after touch

    touchcancel
    • Touch ended touchend

    • Animation
    • Event
      • 过渡完成时触发 transitionend

      • 动画开始时触发 animationstart

      • 一次迭代结束时触发 animationiteration

      • 动画完成时触发 animationend

      事件冒泡,事件捕获

    1. 绑定事件 + 冒泡:bind + 事件名  , 如 bindtap

    2. 绑定事件 + 阻止冒泡:catch + 事件名 , 如 catchtap


    1. 捕获 + 冒泡:capture-bind: + 事件名  , 如 capture-bind:tap

    2. 捕获 + 中断事件 + 取消冒泡:capture-catch: + 事件名  , 如 capture-catch:tap

    事件对象

    • BaseEvent( 基础事件,所有事件的父类 )

      • 同 target

      • id ( String - 事件源的id )

      • tagName ( String - 当前组件的类型 )

      • dataset ( Object - 事件源组件上由data-开头的自定义属性集合 )

      • type ( String - 事件类型 )

      • timeStamp ( Integer - 事件生成时的时间戳 )

      • target ( Object - 触发事件的组件的属性 )

      • currentTarget ( Object - 当前组件的属性 )

    • TouchEvent ( 触摸事件 )

      • identifier ( Number - 触摸点的标识符 )

      • pageX ( Number - 距文档左上角 x 轴的距离 )

      • pageY ( Number 距文档左上角 y 轴的距离 )

      • clientX ( Number 距页面可显示区域 x 轴的距离 )

      • clientY ( Number 距页面可显示区域 y 轴的距离 )

      • touches ( Array 停留在屏幕中的触摸点的信息对象集合 )

      • changedTouches ( Array 变化的触摸点信息对象集合 )

    • CustomEvent ( 自定义事件 )

      • detail ( Object - 自定义事件额外的信息 )

    6. wxs 模块

    wxs 文件就是 js 文件,引入 wxs 文件就是引入一个 js 模块( 不能用es6 ),现有两种引入方式

    1. wxml 里引用,使用 <wxs></wxs> 标签
        1.1. module 必填,为当前模块的模块名
        1.2. src 选填,引用 .wxs 文件的相对路径(仅当标签为 单闭合标签 标签的内容为空 时有效)
        1.3. 例

         引入:<wxs></wxs>
      
         自己撸:
         <wxs>
             var foo = '海洋饼干'
             module.exports = {
                 foo
             }
         </wxs>
    2. wxs 里引用,使用 require 引用
        2.1. 例 var tools = require("./tools.wxs")

    7. wxss 样式

    微信版的 css ,几个不同的地方

    1. 添加 自适应尺寸单位 ( rpx )

    2. 添加 导入外联样式 ( @import )

    3. 精减 css选择器,只支持

      1. 类选择器 .class

      2. id选择器 #id

      3. 标签选择器 element

      4. 兄弟选择器 element1,element2

      5. 伪元素选择器 只支持两种 ::after ::before

    8. 自定义组件

    使用自定义组件

    1. 创建自定义组件( 类似于page,但需要在 json 文件中将 component 字段设为 true

      {
        "component": true
      }
    2. 创建组件构造器 ( 构造函数不是page(),而是Component() )

      Component({
        // 组件的对外属性(父组件传的数据)
        properties: {
          msg: {
            type: String,
            value: 'msg',
            // 父组件值改变时触发的回调
            observer: () => {
                console.log('i am change')
            }
          }
        },
        data: {
          componentData: {}
        },
      })
    3. 使用自定义组件( 先要在页面的 json 文件中进行引用声明 )

      // 先引用声明
      "usingComponents": {
          // hybg 标签名
          "hybg": "../component/component" // 相对路径
        }
        
        // 直接使用,需要的话要绑定数据
        <hybg></hybg>

    slot 模板

     1. 单 solt  
        "
        // component
        <view>
            <slot></slot>
        </view>
        
        // page ( hybg 是上面的组件标签名)
        <hybg>
            <view> page 的 slot </view>
        </hybg>
        
        // 效果
        <view>
            <view> page 的 slot </view>
        </view>
        
        "
     2. 多slot
        "
        // 先在 Component 的 js 中添加
        options: {
            // 启用多slot
            multipleSlots: true
          },
        // 多 slot 需要使用不同的 name 来区分
        <view>
            <slot></slot>
            <slot></slot>
        </view>
        
        // page ( hybg 是上面的组件标签名)
        <hybg>
            <view> first -- slot </view>
            <view> last-- slot </view>
        </hybg>
        
        // 效果
        <view>
            <view> first -- slot </view>
            <view> last-- slot </view>
        </view>
        
        "

    父子组件事件(子组件传值)

    // page
    fatherEvent: function(e){
        console.log(e.detail) // 组件传递的自定义信息
      }
    <hybg></hybg>
    
     // component
     tap: function(){
      var myEventDetail = { a: 10} // detail对象,提供给事件监听函数
      var myEventOption = {} // 触发事件的配置选项
      // bubbles 事件是否冒泡
      // capturePhase 事件是否拥有捕获阶段
      // composed 是否可以穿越父组件边界
      this.triggerEvent('hybgEvent', myEventDetail, myEventOption)
    }
    
     <view>触发 hybgEvent 事件</view>

    相关推荐:

    微信小程序入门到实战实例分享(四)

    微信小程序授权获取用户详细信息实例

    微信小程序request请求后台接口php代码实例

    The above is the detailed content of Quick knowledge about WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    使用Python开发微信小程序使用Python开发微信小程序Jun 17, 2023 pm 06:34 PM

    随着移动互联网技术和智能手机的普及,微信成为了人们生活中不可或缺的一个应用。而微信小程序则让人们可以在不需要下载安装应用的情况下,直接使用小程序来解决一些简单的需求。本文将介绍如何使用Python来开发微信小程序。一、准备工作在使用Python开发微信小程序之前,需要安装相关的Python库。这里推荐使用wxpy和itchat这两个库。wxpy是一个微信机器

    小程序能用react吗小程序能用react吗Dec 29, 2022 am 11:06 AM

    小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

    Java语言中的微信小程序开发介绍Java语言中的微信小程序开发介绍Jun 09, 2023 pm 10:40 PM

    微信小程序是一种轻量级的应用程序,可以在微信平台上运行,不需要下载安装,方便快捷。Java语言作为一种广泛应用于企业级应用开发的语言,也可以用于微信小程序的开发。在Java语言中,可以使用SpringBoot框架和第三方工具包来开发微信小程序。下面是一个简单的微信小程序开发过程。创建微信小程序首先,需要在微信公众平台上注册一个小程序。注册成功后,可以获取到

    用Python编写简单的聊天程序教程用Python编写简单的聊天程序教程May 08, 2023 pm 06:37 PM

    实现思路x01服务端的建立首先,在服务端,使用socket进行消息的接受,每接受一个socket的请求,就开启一个新的线程来管理消息的分发与接受,同时,又存在一个handler来管理所有的线程,从而实现对聊天室的各种功能的处理x02客户端的建立客户端的建立就要比服务端简单多了,客户端的作用只是对消息的发送以及接受,以及按照特定的规则去输入特定的字符从而实现不同的功能的使用,因此,在客户端这里,只需要去使用两个线程,一个是专门用于接受消息,一个是专门用于发送消息的至于为什么不用一个呢,那是因为,只

    PHP与小程序的地理位置定位与地图显示PHP与小程序的地理位置定位与地图显示Jul 04, 2023 pm 04:01 PM

    PHP与小程序的地理位置定位与地图显示地理位置定位与地图显示在现代科技中已经成为了必备的功能之一。随着移动设备的普及,人们对于定位和地图显示的需求也越来越高。在开发过程中,PHP和小程序是常见的两种技术选择。本文将为大家介绍PHP与小程序中的地理位置定位与地图显示的实现方法,并附上相应的代码示例。一、PHP中的地理位置定位在PHP中,我们可以使用第三方地理位

    教你如何在小程序中用公众号模板消息(附详细思路)教你如何在小程序中用公众号模板消息(附详细思路)Nov 04, 2022 pm 04:53 PM

    本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了如何在小程序中用公众号模板消息,下面一起来看一下,希望对大家有帮助。

    小程序中文件上传的PHP实现方法小程序中文件上传的PHP实现方法Jun 02, 2023 am 08:40 AM

    随着小程序的广泛应用,越来越多的开发者需要将其与后台服务器进行数据交互,其中最常见的业务场景之一就是上传文件。本文将介绍在小程序中实现文件上传的PHP后台实现方法。一、小程序中的文件上传在小程序中实现文件上传,主要依赖于小程序APIwx.uploadFile()。该API接受一个options对象作为参数,其中包含了要上传的文件路径、需要传递的其他数据以及

    苏州健康码的小程序叫什么苏州健康码的小程序叫什么Oct 24, 2022 am 09:47 AM

    苏州健康码的小程序叫“苏康码”,它是苏州市疫情防控指挥部指定的通行服务码,疫情防控期间在全市范围内通用,可以作为广大民众日常出行的重要凭证,同时作为防疫人员查验的主要依据;也是省内所有来苏逗苏人员以及在苏工作学习生活,旅游或临时停留人员申报的键康申报数据为基础,结合相关数据比对后动态生成的个人电子健康凭证。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft