微信小程序开发

界面开发


  1. 使用自己的AppID在开发者工具中创建一个项目


    image.png

       <small>如果不打算上线,只是纯粹练练手,可以选择无AppID</small>

  2. 开发者工具界面介绍
    设备选择区:可以选择各种设备展示样式


    image.png

    Console:和浏览器上的命令终端一样,可以在此输入代码查看结果


    image.png

    Sources:和浏览器的sources一样,显示项目文件
    image.png

    Network:与浏览器一致,可以在此查看网络请求的结果
    image.png

    Storage:可以将一些数据存在这里


    image.png

    AppData:直观展示了程序运行中各数据的值
    image.png

    Wxml:即当前页面的html
    image.png
  3. 编写代码
    点击开发者工具左侧的“编辑”按钮,我们就可以看到整个项目的代码,其中的框架已经自动生成了。最主要的文件就是app.js、app.json、app.wxss。如果不习惯微信开发者工具的编辑器,可以用其他编辑器打开本地文件进行编辑,然后在开发者工具看运行查看效果。
    app.json:项目的配置文件
    {
    "pages":[
    "pages/index/index",
    "pages/logs/logs",
    "pages/userMeta/user",
    "pages/update/update"
    ],
    "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle":"black"
    }
    }
    <small>pages中配置所有页面的路径
    windows中配置的是窗口的样式</small>
    app.wxml:全局样式文件,类似于css
    /app.wxss/
    .container {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 200rpx 0;
    box-sizing: border-box;
    }
    <small>每个页面都可以单独创建样式文件,这些样式文件的作用范围只针对于它们的同名js文件</small>
    app.js:全局的js代码,包括一些生命周期
    //app.js
    App({
    onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    },
    onShow: function () {
    console.log("App生命周期函数——onShow函数");
    },
    onHide: function () {
    console.log("App生命周期函数——onHide函数");
    },
    onError: function (msg) {
    console.log("App生命周期函数——onError函数");
    }
    getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
    typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
    //调用登录接口
    wx.login({
    success: function () {
    wx.getUserInfo({
    success: function (res) {
    that.globalData.userInfo = res.userInfo
    typeof cb == "function" && cb(that.globalData.userInfo)
    }
    })
    }
    })
    }
    },
    globalData:{
    userInfo:null
    }
    })
    <small>
    onLaunch:当小程序初始化完成时触发
    onShow:当小程序启动,或从后台进入前台是触发
    onHide:当小程序从前台进入后台时触发
    onError:当小程序发生脚本错误,或api调用错误时触发
    getUserInfo:自定义的一些函数,可以被调用
    globalData:用于存储数据的对象
    </small>
    其他页面:写法与全局文件类似,都可以有json文件、js文件和Wxml文件,下面是index页面代码展示
    index.js
    //index.js
    //获取应用实例
    import service from '../../services/service';
    var app = getApp()
    Page({
    data: {
    motto: 'Hello World',
    userInfo: {},
    inputValue: null
    },
    //事件处理函数
    bindViewTap: function() {
    wx.navigateTo({
    url: '../logs/logs'
    })
    },
    bindKeyInput: function(e) {
    this.setData({
    inputValue: e.detail.value
    })
    },
    submit: function() {
    wx.navigateTo({
    url: '../userMeta/user?id=' + this.data.inputValue
    })
    },
    updateClick: function() {
    wx.navigateTo({
    url: '../update/update'
    })
    },
    onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
    //更新数据
    that.setData({
    userInfo:userInfo
    })
    })
    }
    })
    index.wxml

    <view class="container">
    <view bindtap="bindViewTap" class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </view>
    <view class="usermotto">
    <text class="user-motto">{{motto}}</text>
    </view>
    <view class="search">
    <input placeholder="请输入用户id" bindinput="bindKeyInput" />
    <button class="button" bindtap="submit">搜索</button>
    </view>
    <view class="change">
    <button class="add">添加用户信息</button>
    <button class="update" bindtap="updateClick">更新用户信息</button>
    </view>
    </view>
    index.wxml
    /index.wxss/
    .userinfo {
    display: flex;
    flex-direction: column;
    align-items: center;
    }

     .userinfo-avatar {
       width: 128rpx;
       height: 128rpx;
       margin: 20rpx;
       border-radius: 50%;
     }
    
     .userinfo-nickname {
       color: #aaa;
     }
    
     .usermotto {
       margin-top: 200px;
     }
    
     .search {
       margin: 1rem;
     }
     .search input {
       width:70%;
       float:left;
       height:2rem;
       line-height:2rem;
        background-color:#999999;
    
     }
     .search .button {
       width: 27%;
       float: right;
       height: 100%;
       line-height: 2rem;
     }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 转载请注明出处, 谢谢! (~ o ~)Y 1月9日,也就是今天,微信推出的“小程序”正式上线。“小程序”是一种无...
    Jimmy_P阅读 14,581评论 52 273
  • 原文:http://gold.xitu.io/entry/57e34d6bd2030900691e9ad7/pro...
    AiPuff阅读 2,552评论 0 3
  • 本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果。这个小程序的首页将会显示欢迎语以及...
    未央大佬阅读 1,206评论 0 1
  • 最近由于公司需求要做小程序开发,而且做h5的前端同事现在都很忙,所以我们移动开发就开始学习这个微信小程序了,...
    无沣阅读 1,764评论 1 4
  • 微信小程序创建项目 我们需要通过开发者工具,来完成微信小程序创建和代码编辑。 开发者工具安装完成后,打开并使用微信...
    c14328d5898b阅读 1,449评论 8 15

友情链接更多精彩内容