找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 小程序开发 查看内容

小程序公用js提取到app.js中调用的实例

作者:模板之家 2018-4-25 09:12 911人关注

index.wxml: view class=datas bindtap=test01 data-id=0 text{{page}}/text /view view class=datas bindtap=test02 data-id=1 text测试2/text /view navigator class=test2 url=/pages/page2/page2跳转到测试页面p ...

index.wxml:

 

				
  1. <view class="datas" bindtap="test01" data-id="0">
  2. <text>{{page}}</text>
  3. </view>
  4.  
  5. <view class="datas" bindtap="test02" data-id="1">
  6. <text>测试2</text>
  7. </view>
  8.  
  9. <navigator class="test2" url="/pages/page2/page2">跳转到测试页面page2</navigator>

index.wxss:

 

				
  1. .datas{
  2. width: 200px;
  3. height: 100px;
  4. background-color: #188eee;
  5. text-align: center;
  6. line-height: 100px;
  7. color: #fff;
  8. margin-top: 20px;
  9. }
  10. .test2{
  11. width: 200px;
  12. height: 50px;
  13. line-height: 50px;
  14. text-align: center;
  15. color: #fff;
  16. background-color: red;
  17. margin-top: 50px;
  18. }

index.js

 

				
  1. var app=getApp();
  2. Page({
  3. data: {
  4. page:"测试数据1"
  5. },
  6. /*调用公共方法test01*/
  7. test01:function(event){
  8. var that=this;
  9. app.test01(event,that);
  10. },
  11. /*调用公共方法test02*/
  12. test02: function () {
  13. app.test02();
  14. }
  15. })

page2.wxml:

 

				
  1. <view class="page2" bindtap="test01" data-id="page2">
  2. {{page}}
  3. </view>

page2.wxss

 

				
  1. .page2{
  2. width: 200px;
  3. height: 100px;
  4. background-color: #188eee;
  5. text-align: center;
  6. line-height: 100px;
  7. color: #fff;
  8. margin-top: 20px;
  9. }

page2.js

 

				
  1. var app=getApp();
  2. Page({
  3. data: {
  4. page: "测试数据1"
  5. },
  6. /*调用公共方法test01*/
  7. test01: function (event) {
  8. var that = this;
  9. app.test01(event, that);
  10. },
  11. })

app.json

 

				
  1. {
  2. "pages":[
  3. "pages/index/index",
  4. "pages/page2/page2"
  5. ],
  6. "window":{
  7. "backgroundTextStyle":"light",
  8. "navigationBarBackgroundColor": "#fff",
  9. "navigationBarTitleText": "WeChat",
  10. "navigationBarTextStyle":"black"
  11. }
  12. }

app.js

 

				
  1. App({
  2.  
  3. test01: function (event, that){
  4. that.data.page= "修改后的page数据";
  5. that.setData({
  6. page: that.data.page
  7. });
  8. console.log(event.currentTarget.dataset.id);
  9. },
  10.  
  11. test02:function(){
  12. console.log("test2");
  13. }
  14. })


路过

雷人

握手

鲜花

鸡蛋
原作者: 模板之家 来自: 网络收集

全部回复(0)