• 【微信小程序】了解小程序的宿主环境


    1. 什么是宿主环境

    宿主环境(host environment)指的是程序运行所必须的依赖环境。例如:

    Android 系统和 iOS 系统是两个不同的宿主环境。安卓版的微信 App 是不能在 iOS 环境下运行的,所以,

    Android 是安卓软件的宿主环境,脱离了宿主环境的软件是没有任何意义的!

    image-20220814215343404

    image-20220814215355789

    2. 小程序的宿主环境

    手机微信是小程序的宿主环境,如图所示:

    image-20220814215453550

    image-20220814215504503

    小程序借助宿主环境提供的能力,可以完成许多普通网页无法完成的功能,例如:微信扫码、微信支付、微信登录、地理定位、etc…

    3. 小程序宿主环境包含的内容

    ① 通信模型

    ② 运行机制

    ③ 组件

    ④ API

    1.通信模型

    1. 通信的主体

    小程序中通信的主体是渲染层和逻辑层,其中:

    WXML 模板和 WXSS 样式工作在渲染层

    ② JS 脚本工作在逻辑层

    image-20220814215754496

    2. 小程序的通信模型

    小程序中的通信模型分为两部分:

    ① 渲染层和逻辑层之间的通信

    • 由微信客户端进行转发

    ② 逻辑层和第三方服务器之间的通信

    • 由微信客户端进行转发

    image-20220814220130501

    2.运行机制

    1. 小程序启动的过程

    ① 把小程序的代码包下载到本地

    ② 解析 app.json 全局配置文件

    ③ 执行 app.js 小程序入口文件,调用 App() 创建小程序实例

    ④ 渲染小程序首页

    ⑤ 小程序启动完成

    2. 页面渲染的过程

    ① 加载解析页面的 .json 配置文件

    ② 加载页面的 .wxml 模板和 .wxss 样式

    ③ 执行页面的 .js 文件,调用 Page() 创建页面实例

    ④ 页面渲染完成

    3.组件

    1. 小程序中组件的分类

    小程序中的组件也是由宿主环境提供的,开发者可以基于组件快速搭建出漂亮的页面结构。官方把小程序的组

    件分为了 9 大类,分别是:

    ① 视图容器

    ② 基础内容

    ③ 表单组件

    ④ 导航组件

    ⑤ 媒体组件

    ⑥ map 地图组件

    ⑦ canvas 画布组件

    ⑧ 开放能力

    ⑨ 无障碍访问

    2. 常用的视图容器类组件

    ① view

    • 普通视图区域
    • 类似于 HTML 中的 div,是一个块级元素
    • 常用来实现页面的布局效果

    ② scroll-view

    • 可滚动的视图区域
    • 常用来实现滚动列表效果

    ③ swiper 和 swiper-item

    • 轮播图容器组件 和 轮播图 item 组件

    3. view 组件的基本使用

    
    <view class="container1">
      <view>aview>
      <view>bview>
      <view>cview>
    view>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    /* pages/list/list.wxss */
    .container1 view{
      width: 100px;
      height: 100px;
      text-align: center;
      line-height: 100px;
    }
    .container1 view:nth-child(1){
      background-color: green;
    }
    .container1 view:nth-child(2){
      background-color: red;
    }
    .container1 view:nth-child(3){
      background-color: yellow;
    }
    .container1{
      display: flex;
      justify-content: space-around;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    image-20220814222206336

    4. scroll-view 组件的基本使用

    实现如图的纵向滚动效果:

    
    <scroll-view class="container1" scroll-y>
      <view>aview>
      <view>bview>
      <view>cview>
    scroll-view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    /* pages/list/list.wxss */
    .container1 view{
      width: 100px;
      height: 100px;
      text-align: center;
      line-height: 100px;
    }
    .container1 view:nth-child(1){
      background-color: green;
    }
    .container1 view:nth-child(2){
      background-color: red;
    }
    .container1 view:nth-child(3){
      background-color: yellow;
    }
    .container1{
      border:1px solid blue;
      width: 100px;
      height: 100px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    image-20220814223436010

    5. swiper 和 swiper-item 组件的基本使用

    实现如图的轮播图效果:

    
    <swiper class="swiper-container">
      
      <swiper-item>
        <view class="item">aview>
      swiper-item>
      
      <swiper-item>
        <view class="item">bview>
      swiper-item>
      
      <swiper-item>
        <view class="item">cview>
      swiper-item>
    swiper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    /* pages/list/list.wxss */
    .swiper-container{
      height: 100px;
    }
    .item{
      height: 100%;
      line-height: 100px;
      text-align: center;
    }
    swiper-item:nth-child(1) .item{
      background-color: green;
    }
    swiper-item:nth-child(2) .item{
      background-color: red;
    }
    swiper-item:nth-child(3) .item{
      background-color: yellow;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    image-20220815112612590

    image-20220815112623968

    1. swiper 组件的常用属性

    image-20220815113142049

    <swiper class="swiper-container" indicator-dots indicator-color="white" indicator-active-color="red" autoplay interval="3000" circular>
    
    • 1

    image-20220815113158826

    7. 常用的基础内容组件

    ① text

    • 文本组件

    • 类似于 HTML 中的 span 标签,是一个行内元素

    ② rich-text

    • 富文本组件

    • 支持把 HTML 字符串渲染为 WXML 结构

    8. text 组件的基本使用

    通过 text 组件的 user-select 属性,实现长按选中文本内容的效果:

    
    
    <text user-select>123243565767text>
    
    • 1
    • 2
    • 3

    image-20220815115041678

    image-20220815115055519

    9. rich-text 组件的基本使用

    通过 rich-text 组件的 nodes 属性节点,把 HTML 字符串渲染为对应的 UI 结构:

    
    <rich-text nodes=" style='color:red'>标题</h1>">rich-text>
    
    • 1
    • 2

    image-20220815115434996

    应用场景:商品详情页的结构渲染

    10. 其它常用组件

    ① button

    • 按钮组件
    • 功能比 HTML 中的 button 按钮丰富
    • 通过 open-type 属性可以调用微信提供的各种功能(客服、转发、获取用户授权、获取用户信息等)

    ② image

    • 图片组件

    • image 组件默认宽度约 300px、高度约 240px

    ③ navigator

    • 页面导航组件

    • 类似于 HTML 中的 a 链接

    11. button 按钮的基本使用

    
    
    <button>普通按钮button>
    <button type="primary">主色调按钮button>
    <button type="warn">警告按钮button>
    
    
    <button size="mini">普通按钮button>
    <button type="primary" size="mini">主色调按钮button>
    <button type="warn" size="mini">警告按钮button>
    
    
    <button size="mini" plain>普通按钮button>
    <button type="primary" size="mini" plain>主色调按钮button>
    <button type="warn" size="mini" plain>警告按钮button>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    image-20220815120608878

    12. image 组件的基本使用

    
    
    <image>image>
    <image src="/images/1.jpg">image>
    
    • 1
    • 2
    • 3
    • 4
    /* pages/list/list.wxss */
    /*通过边框可以证明image有默认的宽和高*/
    image{
      border:1px solid red;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    image-20220815121754262

    13. image 组件的 mode 属性

    image 组件的 mode 属性用来指定图片的裁剪和缩放模式,常用的 mode 属性值如下:
    image-20220815121821239

    4.API

    1. 小程序 API 概述

    小程序中的 API 是由宿主环境提供的,通过这些丰富的小程序 API,开发者可以方便的调用微信提供的能力,

    例如:获取用户信息、本地存储、支付功能等。

    2. 小程序 API 的 3 大分类

    小程序官方把 API 分为了如下 3 大类:

    ① 事件监听 API

    • 特点:以 on 开头,用来监听某些事件的触发

    • 举例:wx.onWindowResize(function callback) 监听窗口尺寸变化的事件

    ② 同步 API

    • 特点1:以 Sync 结尾的 API 都是同步 API

    • 特点2:同步 API 的执行结果,可以通过函数返回值直接获取,如果执行出错会抛出异常

    • 举例:wx.setStorageSync(‘key’, ‘value’) 向本地存储中写入内容

    ③ 异步 API

    • 特点:类似于 jQuery 中的 $.ajax(options) 函数,需要通过 success、fail、complete 接收调用的结果

    • 举例:wx.request() 发起网络数据请求,通过 success 回调函数接收数据

  • 相关阅读:
    查看锁定SAP账号的IP
    安卓APP源码和设计报告——体育馆预约系统
    mybatis单框架实现多对多连接
    使用Docker安装Apollo并使用SpringBoot连接配置中心
    【数据结构--八大排序】之快速排序
    Python - 函数|异常|模块|包|类和对象|封装|继承
    并发包下的 Unsafe 类
    Android中fastboot devices无法发现设备解决方案和adb remount问题解决
    2023湖南中医药大学计算机考研信息汇总
    【学习日志】2022.11.11 合同矩阵、惯性指数、委托构造、继承控制、=delete、可变参数模板类
  • 原文地址:https://blog.csdn.net/m0_46615524/article/details/126448225