How to get parameters in WeChat applet! ! !

In mini programs, there are many ways to pass parameters between pages. The following are several commonly used methods of passing parameters:

  1. URL parameters: In mini programs, data can be passed between pages through URL parameters. You can carry parameters in the URL of the target page, and then onLoadobtain these parameters in the life cycle function of the target page.

    Jump to the target page in the source page:

    wx.navigateTo({
      url: '/pages/targetPage/targetPage?param1=value1&param2=value2',
    });
    

onLoadGet the parameters in        the target page :

onLoad(options) {
  const param1 = options.param1;
  const param2 = options.param2;
  // ...
}

 2.  Global data or app.js: You can also store the data to be transferred in the mini program app.js, and then obtain it in the target page getApp().

In app.js:

App({
  globalData: {
    param1: 'value1',
    param2: 'value2',
  }
});
const app = getApp();
const param1 = app.globalData.param1;
const param2 = app.globalData.param2;

3. Page stack parameter passing: In the page stack, you can getCurrentPages()get the page stack by calling, and then access the previous page in the stack to pass parameters.

In the source page:

const pages = getCurrentPages();
const prevPage = pages[pages.length - 2];
prevPage.setData({
  param1: 'value1',
  param2: 'value2',
});
wx.navigateBack();

  1. These data can be obtained in the target page in onShowthe life cycle function.

  2. Event parameter passing: If there is event interaction between pages, you can pass data through event parameters. For example, trigger an event on the source page, and then listen to the event on the target page and obtain data.

These are commonly used page parameter transfer methods in mini programs. You can choose the most suitable method to transfer data between pages according to specific scenarios.

Guess you like

Origin blog.csdn.net/m0_74801194/article/details/132544856