小程序部分知识点随记

1.http

wx.request(OBJECT);
例子:
wx.request( {
    url: "http://op.juhe.cn/onebox/weather/query",
    header: {
        "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
//data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" },
    data: Util.json2Form( { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }),
    complete: function( res ) {
        that.setData( {
            toastHidden: false,
            toastText: res.data.reason,
            city_name: res.data.result.data.realtime.city_name,
            date: res.data.result.data.realtime.date,
            info: res.data.result.data.realtime.weather.info,
        });
        if( res == null || res.data == null ) {
            console.error( '网络请求失败' );
            return;
        }
    }
})

2.路由

2.1 保留当前页面,跳转到应用内的某个页面,使用 wx.navigateBack 可以返回到原页面。

    // 注意:目前页面路径最多只能十层。
    wx.navigateTo(OBJECT)

    // 示例 1
    wx.navigateTo({
        url: 'test?id=1'
    })
    //test.js
    Page({
        onLoad: function(option){
            console.log(option.query)
        }
    })

    // 示例二
    wx.navigateTo({
        url:"pages/home/home?type=2"
    });

    // 然后在 home.js 中的 onLoad() 函数中得到值:option.type 就可以得到了,如下:

    onLoad: function (option) {
        this.setData({
            type:option.type,
        });
        console.log(option.type);
    }

2.2 关闭当前页面,跳转到应用内的某个页面。

    wx.redirectTo(OBJECT)
    // 示例
    wx.redirectTo({
        url: 'test?id=1'
    })
    //test.js
    Page({
        onLoad: function(option){
            console.log(option.query)
        }
    })

    // wx.navigateTo() 是保留当前页面,跳转到某个页面,跳转页面后可以返回上一页。
    // wx.redirectTo() 是关闭当前页面,跳转到某个页面,跳转页面后不能返回上一页。
    // 等等

3.全局方法和数据

// 全局的 getApp() 函数可以用来获取到小程序实例。
// other.js
var appInstance = getApp()
console.log(appInstance.globalData) // I am global data

4.数据和事件

4.1 改变数据

    this.setData({
        message:"你好 米娜!"
    })

4.2 获取数据

    this.data.message
    //重点,若要用传参的形式设置数组的值,要用[]将字符串参数值括起来
    //示例一
        Page({
            data: {
            plaintPerList: []
            }
        })
        var type = 'plaintPerList';
        var list = [];
        this.setData({
            [type]: list
        })
    //示例二
        var printPrice = "item["+i+"].print_price";  
            this.setData({  
            [printPrice]: e.detail.value  

4.3 通过事件传递数据 dataset

    // target 触发事件的源组件
    // currentTarget 事件绑定的当前组件
    
    //-----wxml----
    <view class="weui-cell__bd">
        <input class="weui-input" value="{{item.name}}" placeholder="请输入姓名" bindinput="saveEditor"
           data-editor-Type="plaintPerList" data-json-Key="name" data-editor-Index="{{index}}"/>
    </view>
    //-----js----        
    saveEditor: function(e) {
        // type 数组对象名称
        // index 位置
        // jsonKey 数组属性名
        var that = this;
        var type = e.currentTarget.dataset.editorType,
          index = e.currentTarget.dataset.editorIndex,
          jsonKey = e.currentTarget.dataset.jsonKey;
          var list = that.data[type];
        list[index][jsonKey] = e.detail.value;
        // 重新赋值
        that.setData({
        [type]: list
        })
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值