【知识】Uni-app-学习笔记 05

简介: 【知识】Uni-app-学习笔记 05

6、 数据缓存


uni.setStorage


官方文档


将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。


代码演示

<template>
  <view>
    <button type="primary" @click="setStor">存储数据</button>
  </view>
</template>
<script>
  export default {
    methods: {
      setStor () {
        uni.setStorage({
          key: 'id',
          data: 100,
          success () {
            console.log('存储成功')
          }
         })
      }
    }
  }
</script>
<style>
</style>


uni.setStorageSync


将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。


代码演示


<template>
  <view>
    <button type="primary" @click="setStor">存储数据</button>
  </view>
</template>
<script>
  export default {
    methods: {
      setStor () {
        uni.setStorageSync('id',100)
      }
    }
  }
</script>
<style>
</style>


uni.getStorage


从本地缓存中异步获取指定 key 对应的内容。


代码演示

<template>
  <view>
    <button type="primary" @click="getStorage">获取数据</button>
  </view>
</template>
<script>
  export default {
    data () {
      return {
        id: ''
      }
    },
    methods: {
      getStorage () {
        uni.getStorage({
          key: 'id',
          success:  res=>{
            this.id = res.data
          }
        })
      }
    }
  }
</script>


uni.getStorageSync


从本地缓存中同步获取指定 key 对应的内容。


代码演示

<template>
  <view>
    <button type="primary" @click="getStorage">获取数据</button>
  </view>
</template>
<script>
  export default {
    methods: {
      getStorage () {
        const id = uni.getStorageSync('id')
        console.log(id)
      }
    }
  }
</script>


uni.removeStorage


从本地缓存中异步移除指定 key。


代码演示

<template>
  <view>
    <button type="primary" @click="removeStorage">删除数据</button>
  </view>
</template>
<script>
  export default {
    methods: {
      removeStorage () {
        uni.removeStorage({
          key: 'id',
          success: function () {
            console.log('删除成功')
          }
        })
      }
    }
  }
</script>


uni.removeStorageSync


从本地缓存中同步移除指定 key。


代码演示

<template>
  <view>
    <button type="primary" @click="removeStorage">删除数据</button>
  </view>
</template>
<script>
  export default {
    methods: {
      removeStorage () {
        uni.removeStorageSync('id')
      }
    }
  }
</script>


7、上传图片、预览图片


上传图片(uni.chooseImage)


uni.chooseImage方法从本地相册选择图片或使用相机拍照。


案例代码

<template>
  <view>
    <button @click="chooseImg" type="primary">上传图片</button>
    <view>
      <image v-for="item in imgArr" :src="item" :key="index"></image>
    </view>
  </view>
</template>
<script>
  export default {
    data () {
      return {
        imgArr: []
      }
    },
    methods: {
      chooseImg () {
        uni.chooseImage({
          count: 9,
          success: res=>{
            this.imgArr = res.tempFilePaths
          }
        })
      }
    }
  }
</script>


预览图片(uni.previewImage)


结构

<view>
  <image v-for="item in imgArr" :src="item" @click="previewImg(item)" :key="item"></image>
</view>

预览图片的方法

previewImg (current) {
  uni.previewImage({
      current,
      urls: this.imgArr
  })
}


8、条件注释实现跨端兼容


条件编译是用特殊的注释作为标记,在编译时根据这些特殊的注释,将注释里面的代码编译到不同平台。


**写法:**以 #ifdef 加平台标识 开头,以 #endif 结尾。


平台标识


image.png


组件的条件注释


代码演示

<!-- #ifdef H5 -->
<view>
  h5页面会显示
</view>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<view>
  微信小程序会显示
</view>
<!-- #endif -->
<!-- #ifdef APP-PLUS -->
<view>
  app会显示
</view>
<!-- #endif -->


api的条件注释


代码演示

onLoad () {
  //#ifdef MP-WEIXIN
  console.log('这是微信小程序')
  //#endif
  //#ifdef H5
  console.log('这是h5页面')
  //#endif
}

样式的条件注释

代码演示

/* #ifdef H5 */
view{
  height: 100px;
  line-height: 100px;
  background: red;
}
/* #endif */
/* #ifdef MP-WEIXIN */
view{
  height: 100px;
  line-height: 100px;
  background: green;
}
/* #endif */


9、uniapp中的导航跳转


利用声明式导航(navigator)进行跳转


navigator详细文档:文档地址


跳转到普通页面

<navigator url="/pages/about/about" hover-class="navigator-hover">
  <button type="default">跳转到关于页面</button>
</navigator>

跳转到底部导航栏tabbar处页面

<navigator url="/pages/message/message" open-type="switchTab">
  <button type="default">跳转到message页面</button>
</navigator>


利用编程式导航进行跳转


编程式导航跳转文档


利用uni.navigateTo进行导航跳转


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


<button type="primary" @click="goAbout">跳转到about页面</button>

通过navigateTo方法进行跳转到普通页面

goAbout () {
  uni.navigateTo({
    url: '/pages/about/about',
  })
}


利用uni.switchTab进行导航跳转


跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。

<button type="primary" @click="goMessage">跳转到message页面</button>


通过switchTab方法进行跳转


goMessage () {
  uni.switchTab({
    url: '/pages/message/message'
  })
}


利用uni.redirectTo进行导航跳转


关闭当前页面,跳转到应用内的某个页面。

<!-- template -->
<button type="primary" @click="goMessage">跳转到message页面</button>
<!-- js -->
goMessage () {
  uni.redirectTo({
      url: '/pages/message/messag'
  })
}


利用导航跳转传递参数


在导航进行跳转到下一个页面的同时,可以给下一个页面传递相应的参数,接收参数的页面可以通过onLoad生命周期进行接收


传递参数的页面

goAbout () {
  uni.navigateTo({
    url: '/pages/about/about?id=80',
  });
}


接收参数的页面


<script>
  export default {
    onLoad (options) {
      console.log(options)
    }
  }
</script


四、uni-app 组件使用


1、组件的创建


在uni-app中,可以通过创建一个后缀名为vue的文件,即创建一个组件成功,其他组件可以将该组件通过impot的方式导入,在通过components进行注册即可


  • 创建test组件,在component中新建test.vue文件
<template>
  <view>
    这是一个自定义组件
  </view>
</template>
<script>
</script>
<style>
</style>


相关文章
element-plus 树形控件结合下拉列表
要将Element Plus的Tree控件与下拉列表结合起来,可以使用Element Plus的Select控件和Tree控件的插槽来实现。
1126 0
|
Web App开发 移动开发 小程序
扫普通链接二维码打开小程序的踩坑过程...
扫普通链接二维码打开小程序的踩坑过程...
6013 0
扫普通链接二维码打开小程序的踩坑过程...
|
6月前
|
存储 物联网 BI
RFID车辆无感识别通行效率秒过
RFID车辆无感识别基于射频技术,通过车载电子标签与读写器毫秒级通信,实现3–10米远距离、非接触、免停车自动核验,通行仅需1–2秒,识别率≥99.9%,支持智慧停车、园区门禁、高速ETC等多场景,真正“秒过”无忧。(238字)
|
6月前
|
存储 人工智能 JavaScript
工业企业做了十年数据治理,为什么AI还是用不起来
本文揭示工业AI落地的核心瓶颈:数据“可存储”不等于“可理解”。企业虽建有完善系统,但因术语、口径、编码不统一,大模型无法识别业务语义。解决方案是构建企业本体语义体系,实现跨系统概念对齐;并结合智能体架构(AI大脑+技能库+执行环境+知识底座),让AI从“对话机器人”升级为可自主执行任务的“数字员工”,已在SOP指导、CAD审图、包装审核等场景验证实效。(239字)
455 3
|
3月前
|
运维 安全 算法
SSL 证书规则更新|免费与付费证书优缺点拆解,建站选型别踩坑
本文解析SSL证书新规:2026年起有效期缩至199天,免费证仅90天且无主体验证;付费证支持OV/EV、通配符、国密及赔付保障。对比两类证书在安全、信任、运维、合规维度的优劣,助站长按业务场景理性选型。(239字)
|
3月前
|
人工智能 算法
3个月,520万播放,6666个粉丝,普通人如何用AI搞副业?
AI时代,普通人也能轻松做自媒体!本文揭秘“AI自动变现”全流程:从0搭建账号、AI批量生产内容、多平台自动分发,到广告/带货/IP多元变现。无需天赋团队,7天起号,日更3-5条,小投入撬动长期收益。方法已验证,人人可复制。
|
4月前
|
缓存 JSON 程序员
DeepSeek V4 Pro 正式版、Grok 4.6 同夜发布
DeepSeek V4 Pro 正式版(0813)与 Grok 4.6 同夜上线。跑分、价格、选型一次讲清,帮你判断主力 API 要不要换
DeepSeek V4 Pro 正式版、Grok 4.6 同夜发布
|
Java Spring 容器
Spring 核心注解 @Autowired 详解:告别 new,拥抱依赖注入!
本文深入解析Spring核心注解@Autowired,涵盖字段、构造器、Setter注入方式,详解@Qualifier、@Primary、required等高级特性,结合@Value、泛型、条件注解的应用场景,提供最佳实践与性能优化建议,助你掌握依赖注入精髓,提升代码可维护性与可测试性。
1074 7
|
Ubuntu PHP Docker
一个可以运行的Dockerfile_php ,用来创建php容器镜像
该简介描述了一个基于 Dragonwell 8 Ubuntu 的 Docker 镜像,用于构建包含 PHP 7.4 及常用扩展的运行环境。通过更换为阿里云源提升安装速度,配置了 PHP-FPM 并暴露 9000 端口,使用自定义 Dockerfile 构建镜像并成功运行容器。