用户工具

站点工具


mina:page_function

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
mina:page_function [2020/08/24 15:02] – [2.2 Page.prototype.setData()] sabertoymina:page_function [2020/08/24 15:36] (当前版本) – [5. 使用 Component] sabertoy
行 126: 行 126:
  
 ===== -#3 页面间通信 ===== ===== -#3 页面间通信 =====
 +
 +===== -#4 使用 behaviors() =====
 +
 +[[https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/behaviors.html | behaviors]] 可以用来让多个页面有<wrap em>相同的数据字段和方法</wrap>。 [[https://developers.weixin.qq.com/miniprogram/dev/reference/api/Behavior.html|参数说明]]
 +
 +<code javascript>
 +// my-behavior.js
 +module.exports = Behavior({
 +  data: {
 +    sharedText: 'This is a piece of data shared between pages.'
 +  },
 +  methods: {
 +    sharedMethod: function() {
 +      this.data.sharedText === 'This is a piece of data shared between pages.'
 +    }
 +  }
 +})
 +</code> 
 +
 +<code javascript>
 +// page-a.js
 +var myBehavior = require('./my-behavior.js')
 +Page({
 +  behaviors: [myBehavior], //引入其它的 behavior
 +  onLoad: function() {
 +    this.data.sharedText === 'This is a piece of data shared between pages.'
 +  }
 +})
 +</code> 
 +
 +
 +
 +==== - 组件中使用 ====
 +
 +
 +
 +
 +
 +
 +
 +
 +===== -#5 使用 Component =====
 +
 +Page 构造器适用于简单的页面。但对于复杂的页面, Page 构造器可能并不好用。
 +
 +[[https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html | Component]] 构造器的主要区别是:<wrap em>方法需要放在 methods: { } 里面</wrap>
 +
 +<code javascript>
 +Component({
 +  data: {
 +    text: "This is page data."
 +  },
 +  methods: {
 +    onLoad: function(options) {
 +      // 页面创建时执行
 +    },
 +    onPullDownRefresh: function() {
 +      // 下拉刷新时执行
 +    },
 +    // 事件响应函数
 +    viewTap: function() {
 +      // ...
 +    }
 +  }
 +})
 +<code>
  
mina/page_function.1598252538.txt.gz · 最后更改: 2020/08/24 15:02 由 sabertoy