微信小程序-3

news/2024/7/20 4:08:38 标签: 微信小程序, 小程序

一、交互

API - - - 界面 - - - 交互

功能:提示 是否删除

1.wx.showToast 显示消息提示框

<button type="primary" bindtap='clickBtn'>按钮</button>
<input style="margin: 20rpx;height: 60rpx;background: gainsboro;" type="text"/>
  clickBtn(){
    wx.showToast({
      title:'加载中...',
      icon:'loading',
      // image的优先级大于icon
      duration:5000,
      // mask 遮罩层,为true时不可操作界面,页面也不可滚动
      mask:true,
      // 弹窗出现时就会调用
      success:res=>{
      	console.log(res);
      }
    })
  },

2.wx.showModal 显示模态对话框

  clickBtn(){
    wx.showModal({
      title:'是否删除',
      content:'删除之后不可恢复,请谨慎删除',
      // showCancel:false,
      // 箭头函数,不会出现指向问题
      success:res=>{
        console.log(res);
        // comfirm值为确认返回true和取消false
      }
    })
  },

content和editable不要一起使用

  clickBtn(){
    wx.showModal({
      title:'请输入验证码',
      editable:true,
      placeholderText:'请输入...',
      success:res=>{
        console.log(res);
        // comfirm值为true,content值为用户输入的内容
      }
    })
  },

3.wx.showLoading 显示 loading 提示框

  onLoad: function (options) {
    // 一进入页面就会显示
    wx.showLoading({
      title: '加载中...',
      // 加载中不可对页面进行操作
      mask:true,
    })
	// 定时器,1秒后隐藏loading提示框
    setTimeout(()=>{
      wx.hideLoading()
    },1000)
  },

3.wx.showActionSheet 显示操作菜单

  clickBtn(){
    wx.showActionSheet({
      itemList: this.data.listArr,
      success: (res)=> {
        // res.tapIndex得到索引,this.data.listArr是列表
        console.log(this.data.listArr[res.tapIndex])
      },
      fail: (res)=> {
        console.log(res.errMsg)
      }
    })
  },

二、导航栏api接口

平常在json里设置

"navigationBarTitleText":"标题"
  onLoad: function (options) {
    // 动态设置当前页面的标题
    wx.setNavigationBarTitle({
      title: '标题名',
    })
    // 设置页面导航条颜色
    wx.setNavigationBarColor({
      backgroundColor: '#ff0000',
      frontColor: '#000000',
    })
    // 在当前页面显示导航条加载动画
    wx.showNavigationBarLoading()
    setTimeout(() => {
      // 在当前页面隐藏导航条加载动画
      wx.hideNavigationBarLoading()
    }, 2000);
    // 隐藏返回首页按钮
    wx.hideHomeButton()
  },

三、json配置

指南 - - - 配置小程序
框架 - - - 小程序配置 - - - 全局配置app.json - - - 页面配置.json

注意:json文件里不能有注释

  // entryPagePath指定首页,默认index
  "entryPagePath": "pages/demo/demo",
  "pages":[
    "pages/index/index",
    "pages/logs/logs",
    "pages/demo/demo"
  ],

单独页面不显示导航栏

  // 导航栏样式
  // custom 自定义导航栏,只保留右上角胶囊按钮
  "navigationStyle": "custom"

下拉刷新 背景颜色

  // 是否开启当前页面下拉刷新
  "enablePullDownRefresh": true,
  // 刷新窗口的背景色
  "backgroundColor": "#8bc34a",
  // 下拉 loading 的样式(三个点),仅支持 dark / light
  "backgroundTextStyle":"light"

四、tabBar

图标 iconfont 官方图标库 收藏量 ctrl+F搜索 home

  "tabBar": {
    // 字体颜色
    "color": "#cdcdcd",
    // 选中时的颜色
    "selectedColor": "#00ffff",
    "list": [{
      "text": "首页",
      "pagePath": "pages/index/index",
      "iconPath": "/static/icon/home.png",
      "selectedIconPath": "/static/icon/home-fill.png"
    },{
      "text": "我的",
      "pagePath": "pages/logs/logs",
      "iconPath": "/static/icon/user.png",
      "selectedIconPath": "/static/icon/user-lan.png"
    }]
  },

五、api中navigate路由接口与组件的关系

组件 - - - 导航 - - - navigator

<navigator url="/pages/test/test">测试页面</navigator>
<!-- reLaunch关闭所有页面,打开到应用内的某个页面 -->
<!-- 可携带参数,在左下角页面路径那打开页面参数 id=123 -->
<navigator url="/pages/test/test?id=123" open-type="reLaunch">测试页面</navigator>
<!-- 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面,没有携带参数 -->
<navigator url="/pages/test/test?id=123" open-type="switchTab">测试页面</navigator>

或盒子,使用点击事件

<view bindtap="goTest" style="width: 100rpx;height: 100rpx;background: greenyellow;"></view>
  goTest(){
    // wx.reLaunch 关闭所有页面,打开到应用内的某个页面
    wx.reLaunch({
      url: '/pages/test/test',
    })
  },

wx.switchTab - - - 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
wx.redirectTo - - - 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面
wx.navigateTo - - - 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 wx.navigateBack 可以返回到原页面。小程序中页面栈最多十层
wx.navigateBack - - - 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层

六、request请求

1.获取网络请求

<view class="out">
  <view class="box" wx:for="{{listArr}}" wx:key="id">
    <view class="pic">
      <image src="{{item.url}}" mode="aspectFit"/>
    </view>
    <view class="name">姓名:{{item.id}}</view>
  </view>
</view>
.out{padding: 30rpx;}
.out .box{width: 100%;height: 460rpx;border: 1px solid #eee;margin-bottom: 30rpx;}
.out .box .pic{width: 100%;height: 400rpx;}
.out .box .pic image{width: 100%;height: 100%;}
.out .box .name{text-align: center;line-height: 60rpx;}
  data: {
    listArr:[]
  },
  onLoad(options) {
    this.getData();
  },
  // 随机获取猫咪的网络请求
  getData(){
    // console.log(123)
    wx.request({
      // 可以改limit后的数字
      url: 'https://api.thecatapi.com/v1/images/search?limit=2',
      // 需要再详情-本地设置-开启不校验合法域名
      success:res=>{
        console.log(res)
        this.setData({
          listArr:res.data
        })
      }
    })
  },

2.下拉刷新数据

{
  "usingComponents": {},
  "enablePullDownRefresh": true,
  "backgroundColor": "#000",
  "backgroundTextStyle":"light"
}
  data: {
    listArr:[]
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    this.getData();
  },
  getData(){
    // console.log(123)
    wx.request({
      url: 'https://api.thecatapi.com/v1/images/search?limit=2',
      // 需要再详情-本地设置-开启不校验合法域名
      success:res=>{
        console.log(res)
        this.setData({
          listArr:res.data
        })
        // 在页面刷新完后 停止页面下拉刷新
        // API --- 界面 --- 下拉刷新
        wx.stopPullDownRefresh()
        // wx.hideLoading()
      },
      // 接口调用结束的回调函数(调用成功、失败都会执行)
      complete:err=>{
        wx.hideLoading()
      }
    })
  },

  onPullDownRefresh() {
    // console.log(123)
    // 先清空数组
    this.setData({
      listArr:[]
    })
    // 下拉刷新,重新获取数据
    this.getData()
  },

3.触底加载更多数据

onReachBottomDistance 触底距离

{
  "usingComponents": {},
  "enablePullDownRefresh": true,
  "backgroundColor": "#000",
  "backgroundTextStyle":"light",
  "onReachBottomDistance": 200
}
// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr:[]
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    this.getData();
  },
  getData(){
    // console.log(123)
    wx.request({
      url: 'https://api.thecatapi.com/v1/images/search?limit=2',
      // 需要再详情-本地设置-开启不校验合法域名
      success:res=>{
        console.log(res)
        let oldData = this.data.listArr
        // let newData = res.data
        // 让老数据追加新数据,新数据在res.data中
        let newData = oldData.concat(res.data)
        this.setData({
          listArr:newData
        })
        // 在页面刷新完后 停止页面下拉刷新
        // API --- 界面 --- 下拉刷新
        wx.stopPullDownRefresh()
        // wx.hideLoading()
      },
      // 接口调用结束的回调函数(调用成功、失败都会执行)
      complete:err=>{
        wx.hideLoading()
        wx.hideNavigationBarLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // console.log(123)
    // 先清空数组
    this.setData({
      listArr:[]
    })
    // 下拉刷新,重新获取数据
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showNavigationBarLoading()
    // need重新编译
    console.log('触底了');
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

4.其他参数

get请求,data传参

  onLoad: function (options) {
    this.getData();
  },
  getData(){
    wx.request({
      url: 'https://api.thecatapi.com/v1/images/search',
      method:'get',
      data:{
        limit:10
      },
      success:res=>{console.log(res)}
    })
  },

post请求

  getData(){
    wx.request({
      url: 'http://jsonplaceholder.typicode.com/posts',
      method:'post',
      // header里的参数在调试器Network下headers的RequestHeaders里可以看到
      header:{'content-type':'application/json','token':123123},
      data:{
        limit:10,
        name:'wangwu'
      },
      success:res=>{console.log(res)}
    })
  },

七、小案例

<!-- 双向绑定model, bindconfirm点击完成按钮/回车时触发 -->
<input type="text" model:value="{{iptVal}}" bindconfirm="onSubmit" placeholder="请输入标题" style="background: #eee;margin: 30rpx;"/>
<view class="box" style="margin: 30rpx;">
  <view class="row" wx:for="{{listArr}}" wx:key="_id" style="border-bottom: 1px solid #eee;padding: 10rpx;">
    <view class="title">标题:{{item.title}}</view>
    <view class="time">时间:{{item.posttime}}</view>
  </view>
</view>
  data: {
    iptVal:'',
    listArr:[]
  },

  onLoad: function (options) {
    this.getData();
  },
  getData(){
    wx.request({
      // url: 'https://console-docs.apipost.cn/preview/8b23f100b39a63c5/617499421662264b',
      url:'https://tea.qingnian8.com/demoArt/get',
      method:'POST',
      header:{'Content-Type':'application/json'},
      data:{
        num:3,
        page:1
      },
      success:res=>{
        console.log(res);
        this.setData({
          listArr:res.data.data
        })
      }
    })
  },
  onSubmit(){
    // console.log(this.data.iptVal);
    wx.request({
      url: 'https://tea.qingnian8.com/demoArt/add',
      method:'POST',
      header:{'Content-Type':'application/json'},
      data:{
        title:this.data.iptVal
      },
      success:res=>{
        console.log(res);
        if(res.data.errCode!=0){
          // 如果没有成功,返回 不执行下面代码
          return;
        }
        this.setData({
          iptVal:''
        })
        this.getData();
        wx.showToast({
          title: res.data.errMsg,
        })
      }
    })
  },

八、自定义组件Component

框架 - - - 框架接口 - - - 自定义组价
新建文件夹(和pages同级),新建文件夹,右键新建Component
在这里插入图片描述

在json中引入component组件

{
  "usingComponents": {
    "MyHeader":"/component/MyHeader/MyHeader"
  }
}
<!--component/MyHeader/MyHeader.wxml-->
<view class="header">
  <view class="big">头部大标题</view>
  <view class="small">小标题</view>
</view>
/* component/MyHeader/MyHeader.wxss */
.header{text-align: center;padding: 30rpx;background: #eee;}
.header .big{font-size: 52rpx;}
.header .small{font-size: 32rpx;margin-top: 20rpx;color: #666;}


修改组件的内容

<!--component/MyHeader/MyHeader.wxml-->
<view class="header">
  <view class="big">头部大标题</view>
  <view class="small">小标题{{name}}</view>
</view>
// component/MyHeader/MyHeader.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    name:{
      type:String,
      value:"-----"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

  }
})

传参,若没有传参,使用默认值value:“-----”,传参使用传参值

<MyHeader name='-首页'></MyHeader>

http://www.niftyadmin.cn/n/5095142.html

相关文章

复苏拐点信号出现,MCU厂家停止杀价清库存 | 百能云芯

根据台湾《经济日报》的报道&#xff0c;半导体库存调整出现了一些积极的信号。微控制器&#xff08;MCU&#xff09;市场是首个经历价格下滑压力的领域&#xff0c;而中国厂家近期陆续停止了杀价清库存的策略&#xff0c;甚至在某些产品上提高了价格。MCU广泛应用于消费电子、…

AndroidStudio模拟器使用rootAVD ROOT(失败)

下载rootAVD $ git clone https://github.com/newbit1/rootAVD.git 下载Magist Releases topjohnwu/Magisk GitHub 改名为Magist.zip&#xff0c;然后替换 察看设备 > .\rootAVD.bat ListAllAVDsrootAVD.bat system-images\android-31\google_apis_playstore\x86_64…

扩大减产,NAND Flash市场迎来涨价潮 | 百能云芯

根据TrendForce的最新研究&#xff0c;NAND Flash市场即将面临一轮全面涨价的浪潮。供应商们通过严格控制产出量&#xff0c;将在第四季实施合约价的涨幅&#xff0c;预计在8%到13%之间。明年除非原厂仍能维持减产策略&#xff0c;且需留意服务器领域对Enterprise SSD需求是否回…

AI全栈大模型工程师(一)大纲

文章目录 解读本课大纲我们的初心大纲解读定自己的目标后记 解读本课大纲 课程大纲的设计初心每部分能给你带来什么做自己的选择&#xff0c;定自己的目标 我们的初心 我们相信&#xff0c;懂 AI、懂编程、懂业务的超级个体&#xff0c;会是 AGI 时代最重要的人。所以我们提…

【贝叶斯分析】计算机科学专业博士作业一

1、 1964 年&#xff0c;一对跨种族夫妇在洛杉矶被判抢劫罪&#xff0c;主要是因为他们的个人资料符合极不可能的情况&#xff0c;而这一资料与目击者的报告相符。据目击者所述&#xff0c;这两名劫匪是&#xff1a; 男人留着小胡子 、男人是黑人 、女人绑着马尾 、女人是金发 …

想要上抖音热搜榜有什么办法?

要上抖音热搜榜&#xff0c;首先需要了解抖音的热搜算法。抖音热搜榜的算法主要基于以下几个因素&#xff1a;视频的播放量、点赞数、评论数、分享数和关注数。其中&#xff0c;播放量和点赞数是最重要的两个因素。因此&#xff0c;要想上抖音热搜榜&#xff0c;需要在这些方面…

Linux C/C++ 嗅探数据包并显示流量统计信息

嗅探数据包并显示流量统计信息是网络分析中的一种重要技术&#xff0c;常用于网络故障诊断、网络安全监控等方面。具体来说&#xff0c;嗅探器是一种可以捕获网络上传输的数据包&#xff0c;并将其展示给分析人员的软件工具。在嗅探器中&#xff0c;使用pcap库是一种常见的方法…

【计网】计算机网络概述

目录 一、计算机网络的概念 二、计算机网络的组成 1、从组成部分上看 2、从工作方式上看 3、从功能组成上看 三、计算机网络的功能 1、数据通信 2、资源共享 3、分布式处理 4、提高可用性 5、负载均衡 四、计算机网络的分类 1、按分布范围 1.广域网 2.城域网 3.…