【微信小程序】开发实践

本文介绍了微信小程序的开发实践,包括项目结构、全局配置app.json、WXML模板的页面渲染、WXSS样式的组件设定以及js交互逻辑。通过学习,可以掌握如何配置小程序的页面路径、窗口表现、网络超时时间,以及理解WXML和WXSS的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上很全的微信小程序项目

1、微信小程序项目结构

│pages                         
│     │index
│     │     index.js           # 页面逻辑
│     │     index.wxml         # 页面渲染
│     │     index.wxss         # 页面样式
│     │test
│     │     test.js
│     │     test.json         # 页面配置
│     │     test.wxml
│     │     test.wxss
│utils                         # 公用方法模块
│     util.js
│app.js                        # 全局逻辑
│app.json                      # 全局配置(页面路径、窗口表现、设置网络超时时间、设置多tab、是否开启Debug)
│app.wxss                      # 公共样式表
│project.config.json           # 工具配置

2、app.json

小程序全局配置(页面路径、窗口表现、设置网络超时时间、设置多tab、是否开启Debug)

{
  "pages":[
    "pages/index/index",
    "pages/test/test"
  ],
  "window":{
    "navigationBarTitleText": "小试开发微信小程序",
    "navigationBarBackgroundColor": "#000000"
  },
  "tabBar": {
    "backgroundColor": "black",
    "color": "white",
    "list": [{
      "pagePath": "pages/index/index",
      "text": "swiper"
    },
    {
      "pagePath": "pages/test/test", 
      "text": "test" 
    }]
  }
}
  • pages:小程序所有页面路径,为了让微信客户端知道小程序页面的目录
  • window:设置小程序的状态栏、导航条、标题、窗口背景色
  • tabBar:设置底部 tab
  • networkTimeout:设置网络超时时间
  • debug:设置是否开启 debug 模式

注:test.json:单个页面配置,只支持windows,会覆盖app.json同样的配置项

{
  "navigationBarTitleText": "test",
  "navigationBarBackgroundColor": "#FF0000"
}

3、WXML 模板:页面渲染

小程序组件
WXML数据绑定

<swiper autoplay="{{autoplay}}" interval="{{interval}}" >
  <block wx:for="{{imgUrls}}" >   
    <swiper-item>
      <image src="{{item}}" class="swiper-image" bindtap="clickbaidu" />
    </swiper-item>
  </block>
</swiper>

<view wx:for="{{list}}" wx:key="arealist">
  <view>{{item.id}},{{item.name}}</view>
</view>

格式同HTML,不同处:

  • 标签名
  • wx:if 属性及 {{ }} 表达式

4、WXSS样式:组件样式

小程序样式

.swiper-image {
  height: 100%;
  width: 100%
}

5、js交互逻辑

小程序API
WXML事件

Page({
  data: {
    imgUrls: [
      "http://v.leleketang.com/dat/ps/la/k/thumb/25052.jpg",
      "http://v.leleketang.com/dat/ps/ma/k/thumb/24695.jpg",
      "http://v.leleketang.com/dat/ps/la/k/thumb/25067.jpg"
    ],
    autoplay: true,    
    interval: 3000, 
  },

  onLoad: function () {
    var that = this
    wx.request({
      url: 'https://www.leleketang.com/uc/ajax/area_get.php?id=110100',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console
        that.setData({
          list: res.data.data,
        })
      }
    })
  },

  clickbaidu: function(){
    wx.switchTab({
      url: '../test/test'
    })
  }
})

效果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值