微信小程序开发(五)小程序代码组成2

news/2024/7/20 3:08:52 标签: 微信小程序, 小程序

小程序>微信小程序开发(五)小程序代码组成2

为了进一步加深我们对小程序基础知识的了解和掌握,需要更进一步的了解小程序的代码组成以及一些简单的代码的编写。

参考小程序官方的的代码组成文档:https://developers.weixin.qq.com/ebook?action=get_post_info&docid=000ace6c9603786b008636f2e56c0a

1. JSON 配置

JSON 是一种数据格式,并不是一门语言。使用JSON可以方便到的对小程序进行项目配置。

请添加图片描述
在 ·app.json· 文件中的pages 项下添加一行 pages/wsml/index,项目左侧的文件区就会自动生成wxml 文件,里面包含index.js,index.json,index,wxml,index.wxss 文件。wxml 文件对应在小程序上就是一个页面文件,我们可以在此页面文件中添加代码,在小程序的页面中就会显示出来。

2. WXML 模板

  • 例:一个简单的文本标签,text 标签
<text>hello</text>
<text>world</text> 
  • 例:view 中包含了 text 标签
<view>
  <text>hello world</text>
</view>
  • 例:图片标签
<image class="userinfo-avatar" src="../../image/avatar.png" style="width: 40px; height: 40px;" mode="aspectFit"></image> 

在这里插入图片描述
注意这里的文件路径不要写错,否则找不到资源,图片也将不会正常显示。

  • 例:数据绑定

index.wxml

<text>当前时间:{{time}}</text> 

index.json

// pages/wxml/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: (new Date()).toString(),

    length: 6,
    
    array: [0, 1, 2],

    array1: [{
      message: 'go home',
    }, {
      message: 'have a dinner'
    }],

    objectArray: [
      {id: 5, unique: 'unique_5'},
      {id: 4, unique: 'unique_4'},
      {id: 3, unique: 'unique_3'},
      {id: 2, unique: 'unique_2'},
      {id: 1, unique: 'unique_1'},
      {id: 0, unique: 'unique_0'},
    ],
    numberArray: [1, 2, 3, 4],

    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-06-18'
    },
    
  },

  switch: function(e) {
    const length = this.data.objectArray.length
    for (let i = 0; i < length; ++i) {
      const x = Math.floor(Math.random() * length)
      const y = Math.floor(Math.random() * length)
      const temp = this.data.objectArray[x]
      this.data.objectArray[x] = this.data.objectArray[y]
      this.data.objectArray[y] = temp
    }
    this.setData({
      objectArray: this.data.objectArray
    })
  },
  addToFront: function(e) {
    const length = this.data.objectArray.length
    this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray)
    this.setData({
      objectArray: this.data.objectArray
    })
  },
  addNumberToFront: function(e){
    this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray)
    this.setData({
      numberArray: this.data.numberArray
    })
  },
  
})

在 inde.js 的data中,添加time的等定义。在wxml中可以获取到time的值,这称作数据绑定。

  • 条件语句
<view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view>
  • block标签
<block wx:if="{{true}}">
  <view> view1 </view>
  <view> view2 </view>
</block>
  • 列表渲染
wx:for <view wx:for="{{array}}"
  {{index}}: {{item}}
</view> 

<view wx:for="{{array1}}">
  {{index}}: {{item.message}}
</view> -->
wx:for-item & wx:for-index 
<view wx:for="{{array1}}" wx:for-index="idx" wx:for-item="itemName">
  {{idx}}: {{itemName.message}}
</view> 
block wx:if & wx:for 
<!-- <block wx:for="{{['张三', '李四', '王五']}}">
  <view> {{index}}: {{item}}</view>
</block> 
  • switch

{{item.id}}

Switch

Add to the front

{{item}}

Add Number to the front

请添加图片描述

  • 模板 template
<view>
    <text> {{item.index}}: {{item.msg}} </text>
    <text> Time: {{item.time}} </text>
</view>
<template name="msgItem">
  <view>
    <text> {{item.index}}: {{item.msg}} </text>
    <text> Time: {{item.time}} </text>
  </view>
</template>

3. WXSS 样式

3.1 WMSS简介

3.2 尺寸单位:rpx

3.3 WXSS引用

@import './test_0.wxss'

3.4 官方样式库

https://github.com/Tencent/weui-wxss

  1. JavaScript

小程序主要使用js脚本语言。我已了解,没了解的可自行了解和学习。


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

相关文章

js牛客网编程练习汇总(加强基础--待更新)

1、要求在页面上渲染出一个直角三角形&#xff0c;三角形换行要求使用"br"实现。三角形如下&#xff1a; * ** *** var triangle document.querySelector(.triangle);// 补全代码var str for (var i 1; i < 4; i) {for (var j 0; j < i; j) {str *}str &…

CSS3新增属性( 过渡、变形和动画)

文章目录一、过渡1、transition-property2、transition-duration3、transition-timing-function4、transition-delay二、变形1、transform2、2D变形2.1、平移&#xff08;translate&#xff09;2.2、缩放&#xff08;scale&#xff09;2.3、倾斜&#xff08;shew&#xff09;2.…

分布式光伏储能系统的优化配置方法(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

hive自定义函数

hive自定义函数 hive内置的函数满足不了所有的业务需求&#xff0c;可以考虑自己定义函数 UDF&#xff1a;一对一输出(upper) UDTF&#xff1a;一对多输出 (lateral view explode) UDAF&#xff1a;多对一输出(count, max, min) 自定义UDF 用java实现一个UDF 引入依赖 …

基于Java与JSP的文件上传和下载

概念 当用户在前端页面点击文件上传后&#xff0c;用户上传的文件数据提交给服务器端&#xff0c;实现保存。 文件上传步骤 提交方式&#xff1a; 提供form表单&#xff0c;method必须是post。因为post请求无数据限制。 <form method"post"></form>…

FAM alkyne,6-isomer,478801-49-9,6-炔基-羧基荧光素

FAM alkyne,6-isomer&#xff0c;6-FAM-alkyne&#xff0c;6-炔基-羧基荧光素产品规格&#xff1a;1.CAS号&#xff1a;478801-49-92.分子式&#xff1a;C24H15NO63.分子量&#xff1a;413.394.包装规格&#xff1a;5mg&#xff0c;10mg&#xff0c;25mg&#xff0c;包装灵活&a…

[python课程设计1]学生成绩管理系统

引言本课程设计使用数据库&#xff0c;熟悉了Python语言操作数据库&#xff0c;对数据库的增删改查&#xff0c;实现Qt designer界面设计以及excel表格的读写&#xff0c;代码通俗易懂&#xff0c;方便对所学知识的掌握。需求分析用类对学生成绩、代码封装使得操作使用简单&…

数据分析与SAS学习笔记2

SAS在企业使用的情况&#xff1a; SAS是一个很昂贵的商业软件。在企业中使用SAS比较多&#xff0c;在企业界中是比较流行&#xff0c;在学术界使用R比较多。 SAS简介&#xff1a;统计分析系统 处理生物分析数据。 SAS成为统计领域的国际标准软件&#xff0c;服务全球50000多家…