uni-app:基础组件 (上)

简介: 本文介绍了uni-app中多个组件的使用方法,包括存储操作、图标展示、按钮样式、表单输入、导航跳转和输入框控制等。通过具体代码示例展示了如何设置存储键值、使用不同类型的按钮、实现表单提交与重置功能、控制输入框的显示与清除等功能。

引言

在现代应用开发中,跨平台解决方案越来越受到欢迎,uni-app 正是这样一个强大的框架。它能够帮助开发者快速构建高效的移动应用。本文将深入探讨 uni-app 中的多个常用组件,包括数据存储、图标展示、按钮样式、表单输入、导航跳转和输入框控制等。通过具体的代码示例,我们将逐步展示如何利用这些组件提升应用的用户体验和功能性。无论您是新手还是有经验的开发者,这篇文章都将为您提供实用的技巧和深入的理解。

storage

```Plain Text

<script setup>
  const setData = () => {
    uni.setStorage({
      key: 'storage_key',
      data: 'hello',
      success: function() {
        console.log('success');
      }
    });
  }
  const getData = () => {
    const a = uni.getStorage({
      key: 'storage_key',
      success(res) {
        console.log(res.data)
      }
    })
  }
</script>
```

icon

<icon type="success"></icon>

image.png

<!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 -->
<template>
  <view>
    <view class="uni-padding-wrap uni-common-mt">
     <view class="content">
     <progress class="con-pro" :percent="percentVal" show-info   stroke-width="8"/>
          </view>
        <view class="progress-control">
        <button type="primary" @click="setProgress">设置进度</button>
        <button type="warn" @click="clearProgress">清除进度</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
    data() {
      return {
        percentVal:10,
      }
    },
    methods: {
      setProgress() {
        let num  = Math.ceil(Math.random())*10
        this.percentVal +=num
      },
      clearProgress() {
        this.percentVal =0
      }

    },
  }
</script>
<template>
  <view class="page">
    <view class="item" v-for="(value,index) in iconType" :key="index">
      <icon :type="value" size="26"></icon>
      <text>{{value}}</text>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        iconType: ['success', 'success_no_circle', 'info', 'warn', 'waiting', 'cancel', 'download', 'search',
          'clear'
        ],
        
      }
    }
  }
</script>

button

<template>
    <view class="page">
        <button class="my_btn" size="default" type="default" hover-class="is-hover">
            click me
        </button>
    </view>
</template>
<style scoped>
        .my_btn {
        color: red;
        backgroundColor: #1AAD19;
        borderColor: #1AAD19;
    }
    .is-hover {
        color: rgba(255, 255, 255, 0.6);
        background-color: #179b16;
        border-color: #179b16;
    }
</style>


navigate

几个跳转的区别
uni.navigateTo()
保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。

uni.redirectTo(OBJECT)
关闭当前页面,跳转到应用内的某个页面。

uni.reLaunch(OBJECT)
关闭所有页面,打开到应用内的某个页面。

uni.navigateBack(OBJECT)
关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。

uni.switchTab专门用于跳转到 tabBar 页面,并且可以刷新到最新的状态 
uni.navigateTo({
  url: 'B?id=1'
});

checked

<template>
  <view>
    <view class="uni-list">
      <checkbox-group @change="checkboxChange">
        <label class="uni-list-cell uni-list-cell-pd" v-for="item in items" :key="item.value">
          <view>
            <checkbox :value="item.value" :checked="item.checked" />
          </view>
          <view>{{item.name}}</view>
        </label>
      </checkbox-group>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        title: 'checkbox 复选框',
        items: [{
            value: 'USA',
            name: '美国'
          },
          {
            value: 'CHN',
            name: '中国',
            checked: 'true'
          },
          {
            value: 'BRA',
            name: '巴西'
          },
          {
            value: 'JPN',
            name: '日本'
          },
          {
            value: 'ENG',
            name: '英国'
          },
          {
            value: 'FRA',
            name: '法国'
          }
        ]
      }
    },
    methods: {
      checkboxChange: function(e) {
        var items = this.items,
          values = e.detail.value;
        for (var i = 0, lenI = items.length; i < lenI; ++i) {
          const item = items[i]
          if (values.includes(item.value)) {
            this.$set(item, 'checked', true)
          } else {
            this.$set(item, 'checked', false)
          }
        }
      }
    }
  }
</script>
<style scoped>
  .uni-list-cell {
    display: flex;
    justify-content: flex-start
  }
</style>

css

创建一个css 文件
.my_color{
    color: #d55617;
}
引入
<style scoped>
@import 'test.css'
</style>
使用 
<template>
<view>
    <text class="my_color">hello,world</text>
</view>
</template>


form

<!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 -->
<template>
  <view>
    <view>
      <form @submit="formSubmit" @reset="formReset">
        <view class="uni-form-item uni-column">
          <view class="title">switch</view>
          <view>
            <switch name="switch" />
          </view>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">radio</view>
          <radio-group name="radio">
            <label>
              <radio value="radio1" /><text>选项一</text>
            </label>
            <label>
              <radio value="radio2" /><text>选项二</text>
            </label>
          </radio-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">checkbox</view>
          <checkbox-group name="checkbox">
            <label>
              <checkbox value="checkbox1" /><text>选项一</text>
            </label>
            <label>
              <checkbox value="checkbox2" /><text>选项二</text>
            </label>
          </checkbox-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">slider</view>
          <slider value="50" name="slider" show-value></slider>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">input</view>
          <input class="uni-input" name="input" placeholder="这是一个输入框" />
        </view>
        <view class="uni-btn-v">
          <button form-type="submit">Submit</button>
          <button type="default" form-type="reset">Reset</button>
        </view>
      </form>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
      }
    },
    methods: {
      formSubmit: function(e) {
        console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
        var formdata = e.detail.value
        uni.showModal({
          content: '表单数据内容:' + JSON.stringify(formdata),
          showCancel: false
        });
      },
      formReset: function(e) {
        console.log('清空数据')
      }
    }
  }
</script>
<style>
  .uni-form-item .title {
    padding: 20rpx 0;
  }
</style>

input

<!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 -->
<template>
  <view>
    <view class="uni-common-mt">
      <view class="uni-form-item uni-column">
        <view class="title">可自动聚焦的input</view>
        <input class="uni-input" focus placeholder="自动获得焦点" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">键盘右下角按钮显示为搜索</view>
        <input class="uni-input" confirm-type="search" placeholder="键盘右下角按钮显示为搜索" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">控制最大输入长度的input</view>
        <input class="uni-input" maxlength="10" placeholder="最大输入长度为10" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">实时获取输入值:{{inputValue}}</view>
        <input class="uni-input" @input="onKeyInput" placeholder="输入同步到view中" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">控制输入的input</view>
        <input class="uni-input" @input="replaceInput" v-model="changeValue" placeholder="连续的两个1会变成2" />
      </view>
      <!-- #ifndef MP-BAIDU -->
      <view class="uni-form-item uni-column">
        <view class="title">控制键盘的input</view>
        <input class="uni-input" ref="input1" @input="hideKeyboard" placeholder="输入123自动收起键盘" />
      </view>
      <!-- #endif -->
      <view class="uni-form-item uni-column">
        <view class="title">数字输入的input</view>
        <input class="uni-input" type="number" placeholder="这是一个数字输入框" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">密码输入的input</view>
        <input class="uni-input" password type="text" placeholder="这是一个密码输入框" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">带小数点的input</view>
        <input class="uni-input" type="digit" placeholder="带小数点的数字键盘" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">身份证输入的input</view>
        <input class="uni-input" type="idcard" placeholder="身份证输入键盘" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title">控制占位符颜色的input</view>
        <input class="uni-input" placeholder-style="color:#F76260" placeholder="占位符字体是红色的" />
      </view>
      <view class="uni-form-item uni-column">
        <view class="title"><text class="uni-form-item__title">带清除按钮的输入框</text></view>
        <view class="uni-input-wrapper">
          <input class="uni-input" placeholder="带清除按钮的输入框" :value="inputClearValue" @input="clearInput" />
          <text class="uni-icon" v-if="showClearIcon" @click="clearIcon">&#xe434;</text>
        </view>
      </view>
      <view class="uni-form-item uni-column">
        <view class="title"><text class="uni-form-item__title">可查看密码的输入框</text></view>
        <view class="uni-input-wrapper">
          <input class="uni-input" placeholder="请输入密码" :password="showPassword" />
          <text class="uni-icon" :class="[!showPassword ? 'uni-eye-active' : '']"
            @click="changePassword">&#xe568;</text>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        title: 'input',
        focus: false,
        inputValue: '',
        showClearIcon: false,
        inputClearValue: '',
        changeValue: '',
        showPassword: true
      }
    },
    methods: {
      onKeyInput: function(event) {
        this.inputValue = event.target.value
      },
      replaceInput: function(event) {
        var value = event.target.value;
        if (value === '11') {
          this.changeValue = '2';
        }
      },
      hideKeyboard: function(event) {
        if (event.target.value === '123') {
          uni.hideKeyboard();
        }
      },
      clearInput: function(event) {
        this.inputClearValue = event.detail.value;
        if (event.detail.value.length > 0) {
          this.showClearIcon = true;
        } else {
          this.showClearIcon = false;
        }
      },
      clearIcon: function() {
        this.inputClearValue = '';
        this.showClearIcon = false;
      },
      changePassword: function() {
        this.showPassword = !this.showPassword;
      }
    }
  }
</script>

属性

focus  自动 聚焦
confirm-type="search"  为搜索 
<input class="uni-input" placeholder="带清除按钮的输入框" :value="inputClearValue" @input="clearInput" />

  <view class="uni-form-item uni-column">
        <view class="title"><text class="uni-form-item__title">可查看密码的输入框</text></view>
        <view class="uni-input-wrapper">
          <input class="uni-input" placeholder="请输入密码" :password="showPassword" />
          <text class="uni-icon" :class="[!showPassword ? 'uni-eye-active' : '']"
            @click="changePassword">&#xe568;</text>
        </view>
      </view>
    </view>

js

js
   onKeyInput: function(event) {
            this.inputValue = event.target.value
        },

       hideKeyboard: function(event) {
            if (event.target.value === '123') {
                uni.hideKeyboard();
            }
        },
      clearInput: function(event) {
            this.inputClearValue = event.detail.value;
            if (event.detail.value.length > 0) {
                this.showClearIcon = true;
            } else {
                this.showClearIcon = false;
            }
        },

     changePassword: function() {
            this.showPassword = !this.showPassword;
        }


相关文章
|
2天前
|
存储 缓存 关系型数据库
MySQL事务日志-Redo Log工作原理分析
事务的隔离性和原子性分别通过锁和事务日志实现,而持久性则依赖于事务日志中的`Redo Log`。在MySQL中,`Redo Log`确保已提交事务的数据能持久保存,即使系统崩溃也能通过重做日志恢复数据。其工作原理是记录数据在内存中的更改,待事务提交时写入磁盘。此外,`Redo Log`采用简单的物理日志格式和高效的顺序IO,确保快速提交。通过不同的落盘策略,可在性能和安全性之间做出权衡。
1517 4
|
29天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
5天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
492 19
|
2天前
|
存储 SQL 关系型数据库
彻底搞懂InnoDB的MVCC多版本并发控制
本文详细介绍了InnoDB存储引擎中的两种并发控制方法:MVCC(多版本并发控制)和LBCC(基于锁的并发控制)。MVCC通过记录版本信息和使用快照读取机制,实现了高并发下的读写操作,而LBCC则通过加锁机制控制并发访问。文章深入探讨了MVCC的工作原理,包括插入、删除、修改流程及查询过程中的快照读取机制。通过多个案例演示了不同隔离级别下MVCC的具体表现,并解释了事务ID的分配和管理方式。最后,对比了四种隔离级别的性能特点,帮助读者理解如何根据具体需求选择合适的隔离级别以优化数据库性能。
179 1
|
8天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
21天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
9天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
448 5
|
7天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
314 2
|
23天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
25天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2608 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析