欢迎光临,了解微信小程序开发,就上易用通!

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

发布:2018-04-25 09:13浏览: 来源:网络 作者:cola

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. })





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。