- 在VisualStudio中部署GDAL库的C++版本(包括SQLite、PROJ等依赖)
- Android开机流程介绍
- STM32CubeMX教程31USB_DEVICE-HID外设_模拟键盘或鼠标
- 深入浅出Java多线程(五):线程间通信
已经用 uni-app+vue3+ts 开发了一段时间,记录一下日常遇见的问题和解决办法 。
uni-app 是支持多端,如果你想让你的代码,只在部分平台使用,那么就需要用的它的单端处理语法 //#ifdef 和 //#ifndef 等.
//#ifdef xxx
只在xxx平台生效//#ifdef MP-WEIXIN
menuButtonInfo = '微信'
//#endif
//#ifndef xxx
除了xxx平台,其他都生效//#ifndef MP-WEIXIN
menuButtonInfo = '只要不是微信,其他都可以'
//#endif
因为有异形手机屏的存在,最顶部有摄像头,最下面有导航条,为了避免界面内容出现在这些位置,所以每次在界面初始要设置安全边距.
<script setup lang="ts">
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
</script>
<template>
<view class="specification-panel flex-column" :style="{ paddingTop: safeAreaInsets.top + 'px' }">
<!-- 底部导航 -->
<view class="bottomNav" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }"></view>
</view>
</template>
由于微信小程序右上角有微信胶囊,很多时候我们为了保持界面整齐,需要获取微信胶囊的位置,来让我们得元素和它对齐.
// 微信胶囊一定处于安全位置,所以有微信胶囊就拿胶囊的位置,否则再去获取安全边距
export const safeTop = () => {
const { safeAreaInsets } = uni.getWindowInfo()
// 获取胶囊信息 https://uniapp.dcloud.net.cn/api/ui/menuButton.html#getmenubuttonboundingclientrect
let menuButtonInfo = { top: 0 }
//#ifdef MP-WEIXIN
menuButtonInfo = uni.getMenuButtonBoundingClientRect()
//#endif
const top = menuButtonInfo.top || safeAreaInsets?.top
return {
top
}
}
全局组件 目前只能在 src/pages.json 里配置,代码如下:
// 组件自动导入
"easycom": {
// 开启自动扫描
"autoscan": true,
"custom": {
// 使用了uni-ui 规则如下配置
"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue",
// 自定义组件,需要使用正则表达式
"^Weiz(.*)": "@/components/Weiz$1/index.vue"
}
}
使用的时候,直接在界面使用即可,无需再导入.
<WeizCarousel class="categories-banner" size="small" />
有的时候我们需要去拿到界面元素的相关信息,然后进行一些处理,uni-app 提供了相关API,但需要和 vue3 配合使用 。
<script setup lang="ts">
import { getCurrentInstance } from 'vue'
const instance = getCurrentInstance()
const getNodeInfo = () => {
const query = uni.createSelectorQuery().in(instance)
query
.select('.similar') // 获取界面元素,也可以传id
.boundingClientRect((data) => {
const nodeInfo: UniApp.NodeInfo = data as UniApp.NodeInfo
console.log(nodeInfo)
})
.exec()
}
</script>
是的你没看错,不需要给元素设置 ref 。
url 跳转界面有两种方式,一种是使用 navigator 标签,一种是使用 uni.navigateTo 方法。 需要注意的是url有长度限制,太长的字符串会传递失败,而且参数中出现空格等特殊字符时需要对参数进行编码,如使用 encodeURIComponent 等.
uni.navigateTo({
url: 'pages/test?id=1&name=uniapp'
});
或者 。
<script setup lang="ts">
const item = ref({ id: 1, name: 'uniapp' })
</script>
<template>
<navigator :url="'/pages/test/test?item='+ encodeURIComponent(JSON.stringify(item))"></navigator>
</template>
在 pages/test 界面 。
onLoad: function(option) {
console.log(option.id, option.name)
// 如果传递的是经过编码的参数
const item = JSON.parse(decodeURIComponent(option.item));
}
子组件:
<script setup lang="ts">
// 使用 defineProps 来接受参数,非必要参数使用 xxx? 的形式
defineProps<{
title: string
subTitle?: string
}>()
</script>
<template>
<view class="weiz-title">
<view class="title">{{ title }}</view>
<view class="sub-title">{{ subTitle }}</view>
</view>
</template>
父组件:
// 由于是全局组件,所以无需再引入,如果不是全局组件,需要单独引入 <WeizTitle title="详情" />
如果参数比较复杂,可以直接用 TS 去定义类型,下面举例:
子组件:
<script setup lang="ts">
// 引入参数类型
import type { CategoryItem } from '@/types/api'
// 定义 props 接收数据
defineProps<{
list: CategoryItem[]
}>()
</script>
父组件:
<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
// 引入数据类型
import type { CategoryItem } from '@/types/api'
// 引入接口
import { getCategoryIndexAPI } from '@/api/category'
// 定义响应式数据
const categoryList = ref<CategoryItem[]>([])
// 获取数据方法
const getCategoryList = async () => {
const res = await getCategoryIndexAPI()
// 拿到数据赋值
categoryList.value = res.result
}
// 调用方法
onLoad(() => {
getCategoryList()
})
</script>
<template>
<WeizCategory :list="categoryList" @refresh="getCategoryList" />
</template>
父调子需要子组件通过 defineExpose 暴露方法给父组件,然后父组件拿到子组件实例再去调用子组件方法.
// 定义方法
const getCurrentSpec = () => {
return currentSpec.value
}
// 暴露方法给父组件
defineExpose({
getCurrentSpec
})
可参考章节 TS 相关 - 定义组件实例类型,调用子组件需要先拿到子组件的实例,拿到实例后直接调用即可.
// 拿到子组件实例 WeizCategoryInstance 需要我们去 ts 里定义
const weizCategory = ref<WeizCategoryInstance>()
// 调用子组件实例的方法
weizCategory.value.getCurrentSpec()
子调父方法,需要父组件去给子组件添加自定义事件,然后子组件通过 defineEmits 去触发.
<script setup lang="ts">
const handleUpdate = (value: string) => {
console.log('拿到子组件的传值,并且调用了父组件', value)
}
</script>
<template>
<WeizCategory :list="categoryList" @update="handleUpdate" />
</template>
defineEmits
<script setup lang="ts">
import { ref, defineEmits } from 'vue'
const message = ref('子组件的值')
const popupEmit = defineEmits(['update'])
function sendMessage() {
popupEmit('update', message.value)
}
</script>
<template>
<div>
<button @click="sendMessage">触发父组件方法</button>
</div>
</template>
定义组件实例类型文件 xxx.d.ts 。
// 导入组件
import WeizCardList from '@/components/WeizCardList/index.vue'
// 什么全局类型
declare module 'vue' {
export interface GlobalComponents {
WeizCardList: typeof WeizCardList
}
}
// 导出组件实例类型, 需要用到 InstanceType
export type CardListInstance = InstanceType<typeof WeizCardList>
在 vue 页面里使用:
// 导入组件实例类型
import type { CardListInstance } from '@/types/components'
// 拿到组件实例
const cardList = ref<CardListInstance>()
// 调用组件实例的方法
cardList.value?.resetData()
最后此篇关于uni-app+vue3会遇到哪些问题的文章就讲到这里了,如果你想了解更多关于uni-app+vue3会遇到哪些问题的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
老是碰到初版制作完成没多久,就整一出说什么要更改整个项目的色彩体系。真的是宝宝心里苦啊. 起初都是通过uni项目自带的uni.scss中定义,在替换页面上对应的css。以便于达到一次性修改整体布局的
小程序录制视频;10-30秒;需要拍摄人脸,大声朗读数字(123456)这种。 1.camera组件 camera页面内嵌的区域相机组件。注意这不是点击后全屏打开的相机 camera只支持小程序
小程序录制视频;10-30秒;需要拍摄人脸,大声朗读数字(123456)这种。 1.camera组件 camera页面内嵌的区域相机组件。注意这不是点击后全屏打开的相机 camera只支持小程序
在实践运用中,经常需要在路由跳转时判断是否需要登录权限,页面跳转时,添加加在if判断。 插件市场也有一些这种插件,配置也稍微复制,大部分朝向vue-router。 注:本次路由封装,只是单纯的判断是否
已经用 uni-app+vue3+ts 开发了一段时间,记录一下日常遇见的问题和解决办法 。 uni-app 中的单端代码 uni-app 是支持多端,如果你想让你的代码,只在部分平台使
微信小程序的订阅消息是小程序的重要能力之一,为实现服务的闭环提供更优的体验。订阅消息我们应该经常见到,比如下单成功之后的 服务通知 ,支付成功后的 支付成功通知 ,都属于小程序的订阅消息。
这是我目前发现较好的uni-app 自定义底部导航栏方法,其他方法的缺点主要是在切换时,要么会闪烁,要么会每点击一下,都会请求一次数据。如果有其他更好的方法,欢迎评论留言,最近才开始用uni-app
一、前言 应用uni-app跨端框架开发APP过程中,遇到以下新增需求: 应用在闪崩或异常退出时,用户希望正在填写的大量信息可以得以保存。当用户再次打开APP时,之前未提交的信息可回显。 刚开始会考虑
一、资源下载 APP应用开发过程中,资源下载是一种常见应用场景。uni-app中应用uni.downloadFile实现文件下载功能。示例代码如下: downLoadFile() { cons
一、问题描述 Guideline 5.1.1 - Legal - Privacy - Data Collection and Storage We noticed that your app requ
一、资源下载 APP应用开发过程中,资源下载是一种常见应用场景。uni-app中应用uni.downloadFile实现文件下载功能。示例代码如下: downLoadFile() { cons
一、问题描述 Guideline 5.1.1 - Legal - Privacy - Data Collection and Storage We noticed that your app requ
到目前为止,我已经在 Quarkus 中使用 smallrye Mutiny 完成了非常基本的工作。基本上,我有一个或两个非常小的网络服务,它们只与网络应用程序交互。这些服务返回 Uni . 现在我正
到目前为止,我已经在 Quarkus 中使用 smallrye Mutiny 完成了非常基本的工作。基本上,我有一个或两个非常小的网络服务,它们只与网络应用程序交互。这些服务返回 Uni . 现在我正
我正在开发一个使用 C++ 库的 java 应用程序。我的应用程序类型是客户端/服务器 RCP(基于 eclipse 插件)。问题是当我尝试将 2 个以上客户端连接到我的服务器时,它总是卡住。奇怪的是
来自The Java® Language Specification:的声明 An exception parameter of a uni-catch clause is never implici
在嵌入式系统中,由于各种合理的原因,趋势仍然没有完全转向多核处理器。 因此,了解单处理器系统使用各种机制和多线程功能的同步行为仍然很重要。此外,每当我接受采访时,他们都会问我有关特定 C 程序在 un
我编写了一个函数,目的是为字符串中的每个字母填充一个带有 uni 代码字符的数组。 但是我返回的结果是一个空值数组。 function rot13(str) { var splitString =
我在尝试在 .sql 数据库中获取数据源时遇到了 mySQL 问题。我正在运行 Ubuntu 15.10 并通过终端使用 mySQL。这是我获取数据库的过程: mysql -u root - p 输入
大家好 今年我们的 uni coursenotes 有一些错误,我只是在考试前复习小测验,附图显示了“不能作为非成员运算符重载实现”的正确答案。 在这里发帖可能无法及时从 uni 论坛得到答案。是我误
我是一名优秀的程序员,十分优秀!