首页 > 编程语言 >微信小程序基础

微信小程序基础

时间:2023-07-14 17:23:06浏览次数:38  
标签:flex wxml 微信 程序 js item 基础 data event

0-demo

demo.wxml

<!--pages/demo/demo.wxml-->
<text>{{10+20}}</text>
<view>{{10>20 ? 'aaaa':'bbbb'}}</view>

<view>{{myName}}</view>

<view id="my-{{ids[0]}}">aaaaaa</view>
<view id="my-{{ids[1]}}">bbbbbb</view>
<view id="my-{{ids[2]}}">cccccc</view>

<view wx:for="{{list}}" wx:key="index">
{{item}}-----{{index}}
</view>

<view wx:for="{{list}}" wx:for-item="myItem" wx:for-index="myIndex" wx:key="myIndex">
{{myItem}}-----{{myIndex}}
</view>

<view wx:if="{{isCreated}}">我是动态创建和删除1111111</view>
<view wx:else>我是动态创建和删除22222222</view>

<view hidden="{{isHidden}}">我是动态隐藏和显示</view>

<button type="primary" bindtap="handleTap">click1</button>
<button type="primary" catchtap="handleTap">click2</button>

demo.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myName:"张三",
    ids:["AAA","BBB","CCC"],
    list:["唱","跳","Rap","打篮球"],
    isCreated:false,
    isHidden:false    
  },

  handleTap(){
    this.setData({
      myName:"李四",
      isCreated:!this.data.isCreated,
      isHidden:!this.data.isHidden
    })
  }
  ......其他  
})

1-todolist

todolist.wxml

<view class="box">
  <input type="text" bindinput="handleInput" value="{{mytext}}"/>
  <button size="mini" bindtap="handleAdd">添加</button>
</view>

<view wx:if="{{datalist.length>0}}">
  <view wx:for="{{datalist}}" wx:key="index" class="list">
    <text>{{item}}</text>
    <button size="mini" bindtap="handleDelete" data-myid="{{index}}">删除</button>
  </view>
</view>

<view wx:else>暂无待办事项</view>

todolist.wxss

input{
  border: 1px solid black;
  height: 50px;
  line-height: 50px;
  border-radius: 10px;
}

.box{
  display: flex;
  flex-direction: row;
}

.list{
  display: flex;
  flex-direction: row;
  justify-items: between;
}

.list text{
  width: 200px;
}

todolist.js

// pages/1-todolist/1-todolist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mytext:"",
    datalist:["111","222","333"]
  },

  handleInput(event){
    // console.log(event.detail.value)
    this.setData({
      mytext:event.detail.value
    })
  },

  handleAdd(){
    this.setData({
      datalist:[...this.data.datalist,this.data.mytext],
      mytext:""
    })
  },

  handleDelete(event){
    // console.log(event.target.dataset.myid)
    // console.log(this.data.datalist.splice(event.target.dataset.myid,1))
    this.data.datalist.splice(event.target.dataset.myid,1)
    this.setData({
      datalist:this.data.datalist
    })
  }
})

2-highlight

highlight.wxml

<view class="box">
  <view wx:for="{{datalist}}" wx:key="index" 
        bindtap="handleTap" data-myid="{{index}}" 
        class="item {{current === index?'active':''}}"
  > 
    <text>{{item}}</text>
  </view>
</view>

highlight.wxss

.box{
  display: flex;
  flex-direction: row;
}

.box .item{
  flex:1;
  text-align: center;
}

.active{
  color:red;
}

highlight.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist:["衣服","裤子","电子"],
    current:0
  },

  handleTap(event){
    // console.log(event.target.dataset.myid)
      this.setData({
        current:event.currentTarget.dataset.myid
      })
  }
})

3-rpx

rpx.wxml

<view class="box"></view>

rpx.wxss

.box{
  width: 750rpx;
  height: 400rpx;
  background-color: yellow;
}

4-wxs

wxs.wxml

<wxs src="./date.wxs" module="handleDate"/>
<wxs src="./goodFilter.wxs" module="goodFilter"/>
<text>{{handleDate(startTime)}}</text>
 
<view>
  <input bindinput="handleInput"/>
  {{mytext}}
  <view wx:for="{{goodFilter(goodsList,mytext)}}" wx:key="index">
    {{item}}
  </view>
</view>

wxs.wxss

input {
  border:2px solid black;
}

date.wxs

function handleDate(time){
  var odate = getDate(time)
  // console.log(odate)
  return odate.getFullYear() + "-" + (odate.getMonth()+1) + "-" + odate.getDate()
}

module.exports = handleDate

goodFilter.wxs

function goodFilter(goodsList,mytext){
  return goodsList.filter(function(good) {
    return good.indexOf(mytext)>-1
  })
}

module.exports = goodFilter

wxs.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mytext:"",
    startTime: 1624676059976,
    goodsList:["aaa","abc","ddd","acc","bcc","abd","bcd","acd"]
  },

  handleInput(event){
    this.setData({
      mytext:event.detail.value
    })
  }
})

5-request

request.wxml

<button type="primary" bindtap="handleAjax">ajax</button>

<view wx:for="{{datalist}}" wx:key="index" class="item">
  <image src="{{item.img}}" mode="widthFix"/>
  <view>{{item.nm}}</view>
</view>

request.wxss

.item{
  overflow: hidden;
  padding:10rpx;
}
.item image{
  width: 200rpx;
  float: left;
}

request.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist:[]
  },

  handleAjax(){
    wx.request({
      url:'https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json?ct=%E7%99%BD%E9%93%B6&ci=363&channelId=4',
      method:"get",
      data:{
        
      },
      success:(res)=>{
        // console.log(res.data.data.hot)
        this.setData({
          datalist:res.data.data.hot
        })
      },
      fail:()=>{

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onl oad(options) {
    this.handleAjax()
  }
})

6-image

image.wxml

<image src="https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg" mode="widthFix"></image>

image.wxss

image{
  width: 300px;
  height: 200px;
}

7-swiper

swiper.wxml

<!--pages/7-swiper/7-swiper.wxml-->
<button bindtap="handleAjax" type="primary">ajax-swiper</button>

<swiper indicator-dots="{{true}}" circular="{{true}}" autoplay="{{true}}" interval="{{3000}}">
  <swiper-item wx:for="{{datalist}}" wx:key="index">
    <image src="{{item.image_url}}" mode="widthFix" style="width: 100%;"/>
  </swiper-item>
</swiper>

swiper.wxss

swiper{
  height: 314rpx;
}

swiper.js

// pages/7-swiper/7-swiper.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datalist:[]
  },

  handleAjax(){
    wx.request({
      url: 'https://api.juooo.com/home/index/getClassifyHome?city_id=0&abbreviation=&version=6.1.40&referer=2',
      success:(res)=>{
        //  console.log(res.data.data.slide_list) 
         this.setData({
          datalist:res.data.data.slide_list
         })
      }
    })
  }
})

8-scrollview

scrollview.wxml

<!--pages/8-scrollview/8-scrollview.wxml-->
<!-- <view style="height: 200px;background-color: yellow;">
  轮播
</view> -->
<view>水平方向</view>
<scroll-view class="box_horizontal" scroll-x="{{true}}" enable-flex="{{true}}"
bindscrolltolower="handleRight">
  <view class="item_horizontal">aaaaaa</view>
  <view class="item_horizontal">bbbbbb</view>
  <view class="item_horizontal">cccccc</view>
  <view class="item_horizontal">dddddd</view>
</scroll-view>

<view>垂直方向</view>
<scroll-view class="box" scroll-y="{{true}}"  
bindscrolltolower="handlescrolltolower" refresher-enabled="{{true}}" 
bindrefresherrefresh="handleRefresh" refresher-triggered="{{isRefresh}}">
  <view>111111111111111</view>
  <view>222222222222222</view>
  <view>333333333333333</view>
</scroll-view>

scrollview.wxss

/* pages/8-scrollview/8-scrollview.wxss */
.box{
  height: 300px;
}

.item{
  height: 300px;
}

.box_horizontal{
  height: 300px;
  display: flex;
  flex-direction: row;
  width: 100vw;
}

.item_horizontal{
  width: 200px;
  flex-shrink: 0;
}

scrollview.js

// pages/8-scrollview/8-scrollview.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefresh:true
  },

  handlescrolltolower(){
    console.log("到底了")
  },

  handleRefresh(){
    console.log("下拉了")
    setTimeout(()=>{
      this.setData({
        isRefresh:false
      })
    },2000)
  },

  handleRight(){
    console.log("到右边了")
  }
})

9-othercomponent

othercomponent.wxml

<!--pages/9-othercomponent/9-othercomponent.wxml-->
<wxs src="./sum.wxs" module="sum"/>
<icon class="icon-box-img" type="warn" size="93"></icon>

<view wx:for="{{checkList}}" wx:key="index" style="display: flex;justify-content: space-around;padding: 10px;">
  <checkbox checked="{{item.isChecked}}" bindtap="handeTap" data-index="{{index}}"/>
  <view>
    <view>{{item.name}}</view>
    <view>价格:¥{{item.price}}</view>
  </view>
  <view>{{item.number}}</view>  
</view>

<view>
  金额计算:{{sum(checkList)}}
</view>

sum.wxs

function sum(checkList) {
  var total = 0
  for (var index = 0; index < checkList.length; index++) {
    if(checkList[index].isChecked){
      total += checkList[index].number * checkList[index].price
    }
  }
  return total
}

module.exports = sum

othercomponent.js

// pages/9-othercomponent/9-othercomponent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkList:[
      {
        id:1,
        name:"aaa",
        price:100,
        number:1,
        isChecked:false
      },
      {
        id:2,
        name:"bbb",
        price:200,
        number:2,
        isChecked:false
      },
      {
        id:3,
        name:"ccc",
        price:300,
        number:3,
        isChecked:false
      }
    ]
  },

  handeTap(event){
    var index = event.target.dataset.index
    // console.log(index)
    this.data.checkList[index].isChecked = !this.data.checkList[index].isChecked
    // console.log(this.data.checkList)
    this.setData({
      checkList:[...this.data.checkList]
    })
  }
})

10-selfComponent

selfComponent.wxml

<!--pages/10-selfComponent/10-selfComponent.wxml-->
<view>自定义组件</view>
<navbar></navbar>

<!-- 父传子 -->
<!-- <navbar list="{{['aaa','bbb']}}"></navbar> -->  

navbar.wxml

<!--components/navbar/navbar.wxml-->
<view class="box">
  <view wx:for="{{list}}" wx:key="index" class="item {{current===index?'active':''}}"
  bindtap="handleClick" data-index="{{index}}">
    {{item}}
  </view>
</view>

navbar.wxss

/* components/navbar/navbar.wxss */
.box{
  display: flex;
  flex-direction: row;
}

.item{
  flex:1;
  height: 50px;
  line-height: 50px;
  text-align: center;
}

.active{
  color:red;
}

navbar.js

// components/navbar/navbar.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list:{
      type:Array,
      value:["正在热映","即将上映"],
    },
    current:{
      type:Number,
      value:0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // datalist:["正在热映","即将上映"],
    // current:0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleClick(event){
      // this.setData({
      //   current:event.target.dataset.index
      // })
      this.triggerEvent("ParentEvent",event.target.dataset.index)
    }
  }
})

11-slot

slot.wxml

<!--pages/11-slot/11-slot.wxml-->
<top-header>
  <button slot="left" bindtap="handleTap">返回</button>
  <button slot="right">首页</button>
</top-header>

<footer wx:if="{{isShow}}"></footer>

slot.js

// pages/11-slot/11-slot.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false
  },

  handleTap(event){
    // console.log(event)
    this.setData({
      isShow:!this.data.isShow
    })
  }
})

slot.json

{
  "usingComponents": {
    "top-header":"../../components/topheader/TopHeader",
    "footer":"../../components/footer/Footer"
  }
}

TopHeader.wxml

<!--components/topheader/TopHeader.wxml-->
<view class="box">
  <slot name="left"></slot>
  <text>topheader</text>
  <slot name="right"></slot>
</view>

TopHeader.wxss

/* components/topheader/TopHeader.wxss */
.box{
  display: flex;
  flex-direction: row;
}

TopHeader.js

// components/topheader/TopHeader.js
Component({
  // 支持多个插槽
  options:{
    multipleSlots:true
  },

  /**
   * 组件的属性列表
   */
  properties: {

  },

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

  },

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

  }
})

Footer.wxml

<!--components/footer/Footer.wxml-->
<view class="box"></view>

Footer.wxss

/* components/footer/Footer.wxss */
.box{
  width: 100%;
  height: 200px;
  position: fixed;
  bottom: 0;
  left:0;
  background-color: red;
}

12-lifecycle

lifecycle.wxml

<!--pages/12-lifecycle/12-lifecycle.wxml-->
<view>抢购倒计时</view>
<count wx:if="{{isCreated}}" bindevent="handleEvent"></count>

lifecycle.json

{
  "usingComponents": {
    "count":"../../components/count/Count"
  }
}

lifecycle.js

// pages/12-lifecycle/12-lifecycle.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCreated:true
  },

  handleEvent(){
    this.setData({
      isCreated:false
    })
  }
})

Count.wxml

<!--components/count/Count.wxml-->
{{count}}

Count.js

// components/count/Count.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  lifetimes:{
    attached:function () {
      this.intervalId = setInterval(()=>{
        if(this.data.count==0){
          this.triggerEvent("event")
          return
        }
        this.setData({
          count:this.data.count-1
        })
      },1000)
    },
    detached:function () {
      clearInterval(this.intervalId)
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    count:10
  },

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

  }
})

 

标签:flex,wxml,微信,程序,js,item,基础,data,event
From: https://www.cnblogs.com/bijian/p/17551790.html

相关文章

  • AI智能识别微信小程序源码-带流量主功能
     AI智能识别微信小程序源码带流量主功能。基于腾讯云ocr识别接口做的识别工具(自动识别图片、证件、车牌、身份证等)。 演示地址:www.runruncode.com/wxapp/19459.html  ......
  • 刷力扣高频SQL50题(基础)总结
    此随笔仅总结个人刷SQL题时,突然不会使用的某函数或某方法,大佬勿看勿喷regexp'正则表达式'一般用于邮箱校验例题:查找拥有有效邮箱的用户select*fromuserswheremailregexp'^[a-zA-Z]+[a-zA-Z0-9_\\./\\-]*@leetcode\\.com$'窗口函数窗口函数讲解函数+over(pa......
  • WeChatDownload丨微信公众号文章下载工具话题文章批量下载
     使用平台:Windows下载安装包别只单纯的看这个工具名字,这真的不是微信下载工具,而是微信公众号文章下载工具,以前也给大家推荐过类似的工具,因为有小伙伴想收藏莫理的文章,今天介绍一款新的,使用特别方便快捷!它分两个软件,一个是下载某一篇文章,另一个是下载某个话题下的所有文章。......
  • 直播app开发搭建,uniapp中微信小程序账号登录
    直播app开发搭建,uniapp中微信小程序账号登录<template><view>    <!--登录适配-->       <!--最新版登录方法--><button             type='primary'@tap="getUserProfile">新      </button>      <......
  • 微信小程序获取环境变量
    微信小程序获取环境变量在微信小程序中,无法直接获取环境变量。但是,我们可以通过其他方式来模拟环境变量的功能。参考用法通过wx.getAccountInfoSync()获取小程序信息,包含小程序appid,小程序版本(环境)。在app.js中设置全局变量//app.jsApp({//全局数据,是否为开发环......
  • C#基础
    重生之再学C# 1、第一章类-----Class参考:https://www.runoob.com/csharp/csharp-class.html定义一个类时,也就定义类的对象由什么组成  和在这个对象上可执行什么操作。对象就是类的实例,构成   类的方法和变量   称为类的成员。 访问标识符<access......
  • 一文帮你搞定H5、小程序、Taro长列表曝光埋点
    对于很多前端同学来说,“埋点”常常是一个不愿面对却又无法逃避的话题。为什么这么说呢,相信很多前端同学都深有体会:首先埋点这个事基本是前端“独享”的,服务端基本不太涉及;其次添加埋点,往往看起来很简单但实际做起来很麻烦,很多时候为了获取一些埋点需要的信息甚至要对已经写好的代......
  • 【第1周】深度学习基础
    一、代码练习1.pytorch基础练习1.1数据定义一般定义数据使用torch.TensorTensor支持各种各样类型的数据,包括:torch.float32,torch.float64,torch.float16,torch.uint8,torch.int8,torch.int16,torch.int32,torch.int64等创建Tensor有多种方法,有:ones,zeros,eye,a......
  • docker基础知识
    前言我正在参加「掘金·启航计划」。Docker是一个开源的容器化平台,它提供了一种轻量级且可移植的方法来打包、分发和运行应用程序。通过使用Docker,开发人员可以将应用程序及其依赖项打包到称为容器的独立单元中,以便在不同的环境中运行,而无需担心环境差异和依赖项冲突。可以说doc......
  • 从零玩转系列之SpringBoot3-基础特性
    一、简介1.前置知识​ ●Java17​ ●Spring、SpringMVC、MyBatis​ ●Maven、IDEA2.环境要求环境&工具版本(orlater)SpringBoot3.1.xIDEA2023.xJava17+Maven3.5+Tomcat10.0+Servlet5.0+GraalVMCommunity22.3+NativeBuildTools0......