小程序框架总结

目录:
        1.框架概述
        2.项目目录结构
        3. json配置
            3.1 全局json文件app.json
            3.2 单个页面json文件page.json

        4.逻辑层
            4.1 全局js文件app.js
            4.2 单个页面js文件page.js
        
        5.视图层
            5.1 全局/ 单个页面  wxss文件(app.wxss/ page.wxss)
            5.2 单个页面文件page.wxml
 1.框架概述
 2.项目目录结构

                
      全局配置主要设置一些全局的应用参数,全局的样式等。
 
3. json配置
       3.1 全局json文件app.json
     
     
  1. /*app.json可配置的参数:
  2. 1.pages:配置程序页面路径
  3. 2.window:设置窗口的表现样式
  4. 3.tabBar:设置导航tab表现样式
  5. 4.networkTimeout:设置网络超时时间
  6. 5.debug:设置是否开启debug模式(开发时使用)
  7. */
  8. {
  9. /*页面配置,全局的页面必须在这里声明,否则后面使用时将找不到*/
  10. "pages":[
  11. "pages/index/index",
  12. "pages/logs/logs",
  13. "pages/music/music",
  14. "pages/game/game"
  15. ],
  16. "window":{
  17. "backgroundTextStyle":"light",//下拉背景字体 loading图片样式
  18. "navigationBarBackgroundColor": "#fff",//导航栏背景颜色
  19. "navigationBarTitleText": "WeChat",//导航栏文字
  20. "navigationBarTextStyle":"black",//导航栏标题颜色
  21. "backgroundColor": "#ffffff",//窗口背景色
  22. "enablePullDownRefresh": true //是否开启下拉刷新
  23. },
  24. "tabBar": {
  25. /*设置导航栏tab选项list*/
  26. "list": [
  27. {
  28. "pagePath": "pages/index/index",//链接路径
  29. "text": "首页",//tab导航文字
  30. "iconPath":"/image/wechat.png",//tab图标
  31. "selectedIconPath":"/image/wechat.png"//选中图标
  32. },
  33. {
  34. "pagePath": "pages/music/music",
  35. "text": "音乐",
  36. "iconPath":"/image/wechat.png",
  37. "selectedIconPath":"/image/wechat.png"
  38. },
  39. {
  40. "pagePath": "pages/game/game",
  41. "text": "游戏",
  42. "iconPath":"/image/wechat.png",
  43. "selectedIconPath":"/image/wechat.png"
  44. },
  45. {
  46. "pagePath": "pages/logs/logs",
  47. "text": "文学",
  48. "iconPath":"/image/wechat.png",
  49. "selectedIconPath":"/image/wechat.png"
  50. }
  51. ],
  52. "borderStyle":"black",//边框颜色
  53. "position":"top"//tab位置,top/bottom
  54. },
  55. "networkTimeout": {
  56. "request": 20000,//wx.request(request请求)的超时时间
  57. "connectSocket": 20000,//wx.connectSocket(socket请求)的超时时间
  58. "uploadFile": 20000,//wx.uploadFile(上传文件)的超时时间
  59. "downloadFile": 20000//wx.downloadFile(下载文件)的超时时间
  60. },
  61. "debug":true //开启debug模式,开发所用
  62. }
        ps:json似乎不能使用注释,这里只是为了方便查看,不可在项目中加入。
            3.2 单个页面json文件page.json
    
    
  1. /*
  2. page.json只能简单的配置window的一些简单样式
  3. */
  4. {
  5. "navigationBarTitleText": "String",//设置导航栏标题文字
  6. "navigationBarTextStyle": "white",//设置导航标题颜色
  7. "navigationBarBackgroundColor": "#000000",//设置导航栏背景色
  8. "backgroundColor": "#ffffff",//设置窗口背景色
  9. "backgroundTextStyle": "dark",//设置下拉背景字体,loading图片样式
  10. "enablePullDownRefresh": false,//设置是否开启下拉刷新
  11. "disableScroll":false //设置是否禁止滑动
  12. }

4.逻辑层
    4.1 全局js文件app.js
    
    
  1. //app()注册小程序,注意不能注册多个
  2. App({
  3. //监听小程序初始化,当小程序初始化完成时会触发发,且全局只触发一次
  4. onLaunch: function() {
  5. },
  6. //小程序启动或者从后台进入前台时触发
  7. onShow: function() {
  8. },
  9. //小程序从前台进入后台时触发
  10. onHide: function() {
  11. },
  12. //小程序发生脚本错误,api调用失败时触发
  13. onError: function(msg) {
  14. console.log(msg)
  15. },
  16. //其他任意用户定义的函数
  17. anyCustomFunc:function(){
  18. },
  19. //用户自定义的全局数据,可以通过var app = getApp()获取app实例,再通过app.globalData.userInfo获取数据
  20. globalData:{
  21. userInfo:' global data',
  22. otherCustomData:'other custom data'
  23. }
  24. })

    4.2 单个页面js文件page.js
   
   
  1. //获取注册的的app实例
  2. var app = getApp()
  3. //注册页面实例
  4. Page({
  5. //页面初始化数据,视图层通过<view>{{text}}</view>绑定
  6. data: {
  7. text: "This is page init data."
  8. },
  9. //监听页面加载
  10. onLoad: function(options) {
  11. },
  12. //监听页面初次渲染完成
  13. onReady: function() {
  14. },
  15. //监听页面显示
  16. onShow: function() {
  17. },
  18. //监听页面隐藏
  19. onHide: function() {
  20. },
  21. //监听页面卸载
  22. onUnload: function() {
  23. },
  24. //用户下拉刷新触发监听
  25. onPullDownRefresh: function() {
  26. },
  27. //用户滑到底部触发监听
  28. onReachBottom: function() {
  29. },
  30. //用户点击右上角分享触发监听
  31. onShareAppMessage: function () {
  32. return {
  33. title: '自定义分享标题',
  34. desc: '自定义分享描述',
  35. path: '/page/user?id=123'
  36. }
  37. },
  38. //事件处理(自定义函数)
  39. viewTap: function() {
  40. //事件触发后,通过setData()更新页面显示数据
  41. this.setData({
  42. text: 'Set some data for updating view.'
  43. })
  44. },
  45. //用户自定义数据,最好与页面初始化数据分开
  46. customData: {
  47. data1: 'custom data'
  48. }
  49. })
 5.视图层
   5.1 全局/ 单个页面  wxss文件(app.wxss/ page.wxss)
        (1)概述
               wxss为wxml的样式文件,大部分特性与css一致

        (2)相对于CSS的不同于扩展
               2.1)  尺寸单位改为了rpx
               2.2)  样式导入改成了 @import "filename.wxss"
            
        (3)选择器
     
   5.2 单个页面文件page.wxml
        (1) 概述
       page.wxml文件定义用户界面(也就是所谓的UI),通过数据绑定和事件绑定从JS逻辑层获取数据更新UI显示,达到动态更新页面的效果。

        (2)数据绑定
      
   
   
  1. //page.js
  2. Page({
  3. data:{
  4. testdata:'hello i am js data!'
  5. }
  6. })
  1. //page.wxml
  2. <view>{{testdata}}</view>
 
        (3)事件绑定
    
    
  1. //page.js
  2. Page({
  3. data:{
  4. testdata:'hello i am js data!'
  5. },
  6. //编写事件响应函数,改变显示数据
  7. tapEvent:function(){
  8. this.setData(
  9. {
  10. testdata:'can you see me?'
  11. }
  12. )
  13. }
  14. })
  15. //page.wxml 通过bindtap指定事件为tapEvent
  16. <button bindtap="tapEvent">点我试试</button>
  17. <view>{{testdata}}</view>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值