Small micro-channel programs - pass values between pages

Applet page pass by value:

1. The value of the forward pass: previous page -> next page

  1. url traditional values
  2. Local store
  3. Global app objects

2. Reverse traditional values: next page -> Back surface

  1. Local store
  2. Global app objects

Forward pass value

1. url traditional values

 To obtain the parameter values ​​required by url option passed by value.

 More details can be accessed applet -navigateTo chapter .

 A page:

wx.navigateTo({
  url: 'test?id=1'
})

If the transmission is a variable, you can look at your own mosaic:

wx.navigateTo({
  url: '../fighting/fighting?' + "gknumber= " + id,
})

B page:

Page({
  data:{
    id:'',
  },
  onLoad: function(option){
    this.setData({
      id:option.id
    })
  }
})

2. Local storage:

About cache, you can access a small program - data cache little understanding.

A page:

wx.setStorageSync('username', 'ddd')

B page:

Page({
  data:{
    username:'',
  },
  onLoad: function(){
   var username = wx.getStorageSync('username')
   this.setData({
       username: username
    })
  }
})

3. The overall app objects

About app object, you can access a small program - registration program for information.

A page:

var app = getApp();
app.username='ddd';

B page:

var app = getApp();
var username = app.username;

To tell you the value of a reverse pass, read the above are several methods that you should know the value of a reverse pass What are the different ways of. Yes, that Tier 2 and Tier 3:

Reverse transfer value

1. Local Storage:

B page:

wx.setStorageSync ( 'username', 'ddd' );
 // Back 
wx.navigateBack ();

A page:

Page({
  data:{
    username:'',
  },
  onShow: function(){
   var username = wx.getStorageSync('username')
   this.setData({
       username: username
    })
  }
})

2. The global app objects

B page:

var app = getApp();
app.username='ddd';

A page:

var app = getApp(); 

Page({
  data:{
    username:'',
  },
  onShow: function(){
   var username = app.username;
   this.setData({
       username: username
    })
  }
})

 

 

Reference Links: https://www.cnblogs.com/sese/p/9469699.html

 

Guess you like

Origin www.cnblogs.com/lfri/p/11979053.html