Home WeChat Applet Mini Program Development Some experiences in WeChat mini program development

Some experiences in WeChat mini program development

Apr 02, 2017 pm 04:37 PM

1: Parameter Method of passing value

1: data -id

We can add data-* attribute to HTML element to pass the value we need. Instructions for use:

(1)Set data-id

<view class="block" bindtap="playTap" data-id="{{modle.id}}">
Copy after login

(2): Value + Pass value

playTap:function(e) {
    const dataset = e.currentTarget.dataset;
    wx.navigateTo({
     url: &#39;../play/index?id=&#39;+ dataset.id
    })
    console.log(dataset.id);
  }
Copy after login

(3): Value

onLoad:function (param) {
  //页面初始化
    this.setData({
      currentId:param.id
    })
}
Copy after login

data-Notes:data- The name cannot have capital letters. Once I found this error after searching for a long time because it had a capital letter. Objects cannot be stored in the data-* attribute

2: Set the method identifier of id to pass the value

Instructions for use:

(1)Set id

<view bindtap=“playTap" id="{{modle.id}}">
Copy after login

(2)Get the value

Through e.currentTarget. id gets the value of the set id, and then passes the value by setting the global object

3: Add parameters and pass values ​​in the navigator

Instructions for use

(1) Passing value: After the navigator attribute url, splice ?id (parameter name) = the value to be passed (if multiple parameters are separated by & and name=value&…….)

<navigator url="../my/my?id={{item.id}}" wx:for="{{modles}}">
Copy after login

(2) Value:

onLoad (params){
    app.fetch(API.detail + params.id,(err,data) => {
    })
  }
Copy after login

2: Data request encapsulation

1. Put all interfaces in a unified js file and export

const api = {
  interface1: &#39;https://........&#39;,
   interface2: &#39;https://.......&#39;,
   interface3: &#39;https://....&#39;,
   .....
}
module.exports = api;
Copy after login

2 :Create a method to encapsulate request data in app.js

fetch(url,data, callback) {
   wx.request({
     url,
     data: data,
     header: {
       &#39;Content-Type&#39;: &#39;application/json&#39;
     },
     success(res) {
       callback(null, res.data);
     },
     fail(e) {
       callback(e);
     }
   })
 },
Copy after login

3: Call the encapsulated method to request data in a sub-page

import API from "../../api/api.js";
const app = getApp();
const conf = {
  data:{
    title:&#39;正在拼命加载中...&#39;,
    loadding:true
  },
  onLoad (){
    app.fetch(API.hot,{},(err,data) => {
    })
  }
Copy after login

Three: Use templates (I found that templates are really a good thing) !)

1: Define the template: name set the name of the template

<template name="homecell">
   <view class="item">
  </view>
 </template>
Copy after login

2: Use the template

First introduce the template

<import src="../../commonXml/homecell.wxml" />
Copy after login

Then use the template is Write the name of the template.. The data needs to be passed through data

<template is="homecell" data="{{item}}"></template>
Copy after login

Four: Array’s more useful attributes and methods

Array.isArray() method is used to determine whether a certain value is Array. If so, returns true, otherwise returns false.

The concat() method combines the passed array or non-array value with the original array to form a new array and returns.

## The #forEach() method executes the provided function (callback function) once for each element of the array. The

join() method joins all elements in the array into a string.

The keys() method returns an iterator of array indices.

map() method returns a new array consisting of the return value of each element in the original array after calling a specified method

pop() method deletes the last element in an array element and returns this element.

push() method adds one or more elements to the end of the array and returns the new length of the array (length attribute value).

toString() returns a string representing the specified array and its elements.

5: Common methods of Object

1 Initialization method

var obj = [];
var obj = new obj();
var obj = Object.create(null);
Copy after login

2 Method of adding elements

dic[“key”] = “value”;
Copy after login

3 Method to delete key

delete dic[“key”];
Copy after login

4 Clear all entries of the word

dic.clear();
Copy after login

5 Delete

delete dic;
Copy after login

6 Method to view all attributes

Object.keys(obj);
Copy after login

All key names of the object are strings, so they can be added or not in quotes. If the key name is a numerical value, it will be automatically converted It is a string. However, if the key name does not meet the conditions of the identification name (for example, the first character is a number, or contains a space or

operator), and it is not a number, you must add quotation marks, otherwise an error will be reported.

7 Read attributes

obj.name || obj[&#39;name&#39;]
Copy after login

Note: The numeric key name cannot use the dot operator (because it will be treated as a decimal point), only the square bracket operator can be used.

8 Check whether

variable is declared

if(obj.name) || if(obj[&#39;name&#39;])
Copy after login

9 The in operator is used to check whether the object contains a certain attribute. If it does, it returns true, otherwise Return false

if ( ‘x&#39; in obj) {return 1}
Copy after login

10 for … in

Loop

Used to traverse all properties of an object

for (var i in obj) {
console.log(obj);
}
Copy after login

11 with statement

Function: Provide some writing convenience when operating multiple properties of the same object

with(obj) {
name1 = 1;
name2 = 2;
}
Copy after login

Equivalent to

bj.name1 = 1;
obj.name2 = 2;
Copy after login

The above is the detailed content of Some experiences in WeChat mini program development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP development experience sharing: experience and suggestions for mastering the implementation of various functions PHP development experience sharing: experience and suggestions for mastering the implementation of various functions Nov 22, 2023 pm 12:02 PM

In the current era of rapid development of the Internet, PHP, as a server-side scripting language, is adopted by more and more developers. PHP has the advantages of being easy to learn, flexible, open source and free, and can quickly develop various websites and web applications. However, as a PHP developer, if you want to stand out in the fierce competition and write efficient and stable code, you also need to master the implementation skills and experience of various functions. First of all, reasonable planning of project architecture is the key to developing PHP applications. A good project structure can provide better code maintainability

Pitfalls stepped on: Go language project development experience and lessons Pitfalls stepped on: Go language project development experience and lessons Nov 03, 2023 am 08:14 AM

Traps that have been stepped on: Go language project development experience and lessons. On the road of software development, every developer will inevitably step on some pitfalls. Of course, this is no exception for Go language developers. This article will share the pitfalls I have encountered during project development using the Go language, hoping to bring some experience and lessons to other developers. Different versions of Go language When using Go language for project development, we must pay attention to the version of Go language. There may be some language differences or API changes between different versions. These

How to use PHP to quickly build WeChat mini program back-end API How to use PHP to quickly build WeChat mini program back-end API Jun 01, 2023 pm 02:01 PM

With the continuous development of the mobile Internet, WeChat applet has become a new type of mobile application software used by more and more people. As the back-end service of the mini program, the establishment of API interface is crucial. As an open source dynamic scripting language, PHP is widely used in Web development, especially in building small Web applications. Therefore, this article will introduce how to use PHP to quickly build a WeChat mini program back-end API. 1. Understand the basic knowledge of back-end construction of WeChat mini programs. Before starting to build WeChat mini programs,

In-depth understanding of Java GUI development experience and suggestions In-depth understanding of Java GUI development experience and suggestions Nov 22, 2023 am 10:10 AM

In-depth understanding of Java GUI development experience and suggestions As a commonly used object-oriented programming language, Java plays a pivotal role in software development. In Java development, the development of GUI (Graphical User Interface) is one of the important skills that need to be mastered in daily work. In GUI development, rich user interface and interactive performance will directly affect the user experience and user satisfaction of the software. Therefore, in-depth understanding

Git code rollback skills: project experience summary Git code rollback skills: project experience summary Nov 02, 2023 pm 01:44 PM

Git is a popular version control tool widely used in the software development process. In the process of developing projects, we often encounter situations where we need to roll back code, because sometimes the new code we write may introduce some problems, or we need to switch to a previous version. In this article, I will summarize some Git code rollback techniques and share my project experience. First, we need to clarify the purpose of rolling back the code. Is it to fix a bug or to switch to an older version? Depending on the purpose, we can use different

Richard Sutton: Experience is the ultimate data of AI, four stages leading to the development of real AI Richard Sutton: Experience is the ultimate data of AI, four stages leading to the development of real AI Apr 09, 2023 am 10:01 AM

Introduction: The development of strong artificial intelligence has been a topic of concern in recent years. Letting AI learn from human perception and behavior rather than simply labeled data has become the focus of many researchers. Among them, how to use the daily life experiences acquired by humans to inspire and build artificial intelligence that can adapt to different environments and interact with the external world has become a new way to explore in some fields. Richard Sutton, known as the father of reinforcement learning, recently proposed the idea of ​​using experience to inspire the development of AI. He divided the process of AI from using data to using experience into four development stages, and proposed the development direction of building real AI (Real AI) in the future. On May 31, 2022, Richard Sutton made a speech at the 2022 Beijing Intelligent Source Conference

Essential resume for PHP programmers: How to highlight your skills and experience Essential resume for PHP programmers: How to highlight your skills and experience Sep 09, 2023 pm 02:58 PM

PHP Programmer Job Resume Essentials: How to Highlight Your Skills and Experience In today's Internet industry, PHP programmer is one of the most popular and sought-after positions. For PHP programmers who are looking for a job or preparing to change jobs, an excellent resume is the key. How to highlight your skills and experience and attract the attention of employers. Here are some tips and suggestions for writing an excellent resume. Short and clear personal introduction At the beginning of your resume, be sure to write a short and clear personal introduction to introduce your background and goals.

Experience and suggestions for in-depth understanding of Java reflection mechanism Experience and suggestions for in-depth understanding of Java reflection mechanism Nov 22, 2023 am 08:13 AM

Experience and suggestions for in-depth understanding of Java reflection mechanism. In Java programming, reflection is a very powerful and flexible feature. It allows programs to inspect and operate the properties and methods of other classes at runtime, and even create objects dynamically. No compile-time fixed type declaration is required. The reflection mechanism provides us with the flexibility and extensibility to implement plug-ins, framework development, dynamic configuration, etc. However, reflection is also a feature that is easily abused and misunderstood. In this article, we will delve into the principles and

See all articles