微信小程序(WXML模板语法、WXSS模板样式、全局和局部配置、网络数据请求)

简介

黑马程序员,学习记录
视频链接:黑马程序员前端微信小程序开发教程,微信小程序从基础到发布全流程_企业级商城实战


---------- WXML模板语法 ----------

1. 数据绑定

  • 数据绑定的基本原则
  • 在data中第一数据
  • 在WXML中使用数据、
  • Mustache语法格式 {{}}
<text>{{info}}</text>
<image src="{{imgSrc}}" alt="" mode="widthFix"></image>
  data: {
      info:'hello wechat',
      imgSrc: '/images/1.jpg'
  }
  • 三元运算
  data: {
      info:'hello wechat',
      imgSrc: '/images/1.jpg',
      randomNum1: Math.random() * 10
  },
<view>
  {{randomNum1 >=5?'数字大于或等于5':'数字小于5'}}
</view>
  • 算数运算
<view>
  {{randomNum2*100}}
</view>
  data: {
      info:'hello wechat',
      imgSrc: '/images/1.jpg',
      randomNum1: Math.random() * 10,
      randomNum2: Math.random().toFixed(2)
  },

2. 事件绑定

2.1 常用事件

image-20230312190919753

2.2 属性列表

image-20230312191004511

2.3 target 和 currenttarger的区别

image-20230312193443304

2.4 bindtap的语法格式

<button type="primary" bindtap="btnTapHandler">按钮</button>
  btnTapHandler(e){
    console.log(e);
  },

2.5 事件处理函数中给data赋值

  • 事件处理函数

  • this.setData({
      ...
    })
    
<button type="primary" bindtap="CountChange">+1</button>
  // +1按钮得到点击事件处理函数
  CountChange(e){
    this.setData({
      count: this.data.count + 1
    })
  },

2.6 事件传参

  • 不能在绑定事件的同时为事件处理函数传递对象
  • 使用 data-* ( *是参数名)
  • 在获取时使用固定语法 e.target.dataset.* ( *是参数名)
image-20230312192244274
<button type="primary" bindtap="btnTap2" data-info="{{3}}">+2</button>
  btnTap2(e){
    // console.log(e);
    this.setData({
      count: this.data.count + e.target.dataset.info
    })
  },

2.7 bindinput 语法格式

  • 用来响应文本框的输入事件
  • e.detail.value 用来获取文本框最新的值
<input bindinput="inputHandler"></input>
 // input 输入框的事件处理函数
  inputHandler(e){
    console.log(e.detail.value);
  },

2.8 文本框和data之间数据同步

  data: {
      msg:'你好'
  },
<input value="{{msg}}" bindinput="inputHandler"></input>
  // input 输入框的事件处理函数
  inputHandler(e){
    // console.log(e.detail.value);
    this.setData({
      msg: e.detail.value
    })
  },
input{
  border: 1px solid #eee;
  margin: 5px;
  padding: 5px;
  border-radius: 3px;
}

3. 条件渲染

3.1 wx:if

  data: {
      type:1
  },
<!-- 条件渲染 -->
<view wx:if="{{type === 1}}"></view>
<view wx:elif="{{type === 2}}"></view>
<view wx:else>保密</view>

3.2 结合<block>使用ws:if

image-20230312194500895
<block wx:if="{{true}}">
  <view>view1</view>
  <view>view2</view>
</block>

3.3 hidden

image-20230312194713985
  data: {
      flag:true
  },
<view hidden="{{flag}}">
  条件为true时 隐藏 元素 否则显示
</view>
  • wx:if 和 hidden对比

image-20230312195055819

4. 列表渲染

4.1 wx:for

  • 它的索引和内容 默认 用 indexitem 表示
  data: {
      arr1:['苹果','华为','小米']
  },
<view wx:for="{{arr1}}">
  索引是:{{index}},内容是: {{item}}
</view>
  • 手动指定索引和当前项的变量*
  • wx:for-index 指定索引名
  • wx:for-item 指定内容名
<view wx:for="{{arr1}}" wx:for-index='idx' wx:for-item='itemName'>
  索引是:{{idx}},内容是: {{itemName}}
</view>

4.2 wx:key

  • 绑定key值时直接写 不用mustache语法
    userList: [
      { id: 1, name: '小红' },
      { id: 2, name: '小黄' },
      { id: 3, name: '小白' }
    ]
<view wx:for="{{userList}}" wx:key="id">
  {{item.name}}
</view>

---------- WXSS模板样式 ----------

1. wxss 和 css的关系

image-20230312200538928

2. rpx

image-20230312200750039
  • rpx 和 px 之间的单位换算*

    1rpx = 0.5px = 1物理像素

    建议用iphone6设计

3. 样式

  • 样式导入
@import "/common/common.wxss";
  • 全局样式 和 局部样式

    app.wxss 为全局样式

    遵循 就近原则 (局部样式权重 >= 全局样式权重 时才会覆盖)!!!

---------- 全局配置 ----------

1. window节点常用配置项

  • app.json 中
  "window":{
    "backgroundTextStyle":"light", // 加载时小圆点的样式   只有 light  和 dark !!!!!
    "navigationBarBackgroundColor": "#2b4b6b",// 导航栏背景色
    "navigationBarTitleText": "ZKY",// 导航栏标题
    "navigationBarTextStyle":"white", // 导航栏标题颜色		这个只有 black 和 white!!!!
    "enablePullDownRefresh": true, // 全局开启下拉刷新  每个页面都有
    "backgroundColor": "#efefef",// 设置背景颜色
    "onReachBottomDistance": 50,// 设置上拉触底距离 px
  },
image-20230312201843579

2. tabbar

  • app.json 中 pagePath 和 text 必填
"tabBar": {
    "list": [{
      "pagePath": "pages/home/home",
      "text": "首页",
      "iconPath": "/images/tabs/home.png",
      "selectedIconPath": "/images/tabs/home-active.png"
    },
    {
      "pagePath": "pages/message/message",
      "text": "消息",
      "iconPath": "/images/tabs/message.png",
      "selectedIconPath": "/images/tabs/message-active.png"
    },
    {
      "pagePath": "pages/contact/contact",
      "text": "联系我们",
      "iconPath": "/images/tabs/contact.png",
      "selectedIconPath": "/images/tabs/contact-active.png"
    }
  ]
  },
  • 组成部分
image-20230312203520509
  • 节点的配置项
image-20230312203546919
  • 每个tab项的配置选项
image-20230312203649983

3. 页面(局部)配置

  • 常用配置项
image-20230312204949584

---------- 网络数据请求 ----------

  • 只能请求 HTTPS 类型的接口
  • 必须将接口的域名添加到信任列表中
  • 域名不能使用 IP 和 localhost
  • 必须经过ICP认证
  • 一个月最多改5次

1. 发送get请求 和 post请求

  • wx.request()
<button bindtap="getInfo">发起get请求</button>

<button bindtap="postInfo">发起post请求</button>
  getInfo(e) {
    wx.request({
      url: 'https://www.escook.cn/api/get',
      method: 'GET',
      data: {
        name: 'zs',
        age: 20
      },
      success: (res) => {
        console.log(res.data);
      }
    })
  },
  postInfo(e) {
    wx.request({
      url: 'https://www.escook.cn/api/post',
      method: 'POST',
      data: {
        name: 'lisi',
        age: 33
      },
      success: (res) => {
        console.log(res.data);
      }
    })
  },

2. 页面刚加载时获取数据

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getInfo()
    this.postInfo()
  },

3. 跳过request合法域名校验

  • 勾上就行 只能在开发和调试中使用!!!!!!
image-20230312211117183

4. 关于跨域和Ajax的说明

image-20230312211248633

----------- 总结 -------------

image-20230312215018561
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kaiyue.zhao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值