uni-app遇到的一些问题

47 篇文章 0 订阅
  • 给placeholder绑定动态变量

placeholder前加:就可以了,后面是变量

<u-input v-model="form.selectDocumentType" class="choiceInput" :placeholder="selectDocumentTypeLabel"/>
  • form表单中关于一条form-item中多个必填项的prop设置

具体解决方法是在中再包裹一层

<u-form-item label="证件类型" prop="selectDocumentType">
	<view>
		 <u-input v-model="form.selectDocumentType" class="choiceInput" :placeholder="selectDocumentTypeLabel"/>
	</view>
	<view class="typeContainer">
		<u-form-item prop="typeNumber">
			<u-input v-model="form.typeNumber" class="choiceInput" />
		</u-form-item>
	</view>
</u-form-item>
  • uview的select组件,数据是对的却一直报Cannot read property ‘children‘ of undefined

是因为children:[]为空,我们可以改成

"children": [
							{

								"value": null,
								"label": ""
							}
						]
  • uview的学习平台

uView: uView UI,是uni-app生态最优秀的UI框架,全面的组件和便捷的工具会让您信手拈来,如鱼得水

uView - 多平台快速开发的UI框架 - uni-app UI框架

  • 自定义导航栏

1、在pages.json文件内,将你想要自定义导航栏的内容修改成如下:“navigationStyle”:“custom”

{
    "path": "pages/buyProduct/add",
    "style": {
		"navigationStyle":"custom"
    }
}

2、在App.vue页面获取高度

onLaunch: function() {
			console.log('App Launch')
			uni.getSystemInfo({
							success: function(e) {
								// #ifndef MP
								Vue.prototype.StatusBar = e.statusBarHeight;
								if (e.platform == 'android') {
									Vue.prototype.CustomBar = e.statusBarHeight + 50;
								} else {
									Vue.prototype.CustomBar = e.statusBarHeight + 45;
								};
								// #endif
					
								// #ifdef MP-WEIXIN
								Vue.prototype.StatusBar = e.statusBarHeight;
								let custom = wx.getMenuButtonBoundingClientRect();
								console.log(e.statusBarHeight)
								console.log(custom)
								Vue.prototype.Custom = custom;
								Vue.prototype.CustomBar = (custom.top - e.statusBarHeight)*2 + custom.height;
								// #endif		
					
								// #ifdef MP-ALIPAY
								Vue.prototype.StatusBar = e.statusBarHeight;
								Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
								// #endif
							}
						})

			initApp();
		}

3、在调整自定义导航栏的pages页面调整

<template>
<view style="height: 100%;display: flex;flex-direction: column;">
?? ??? ?<view class="statusBar" :style="{'height': statusBar+'px'}"></view>
?? ??? ?<view class="content">
?? ??? ??? ?<view class="customBar" :style="{'height': customBar+'px'}">
?? ??? ??? ??? ?<u-icon name="arrow-left" color="#000" size="50" class="customBarIcon" ?@click="returnPage"
?? ??? ??? ??? ??? ?:style="{'margin-top': (customBar-21)/2 +'px'}"></u-icon>
?? ??? ??? ??? ?<text class="coustomBarText">新增进货</text>

?? ??? ??? ??? ?<view class="page">

此处是你之前的页面内容

</view>

</view>
?? ??? ?</view>
</template>
<script>
	import {
		validator
	} from '../../js_sdk/validator/buyProduct.js';

	const db = uniCloud.database();
	const dbCollectionName = 'buyProduct';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			return {
				statusBar: 0,
				customBar: 0,
			}
		},
		onLoad() {
			this.statusBar = this.StatusBar;
			this.customBar = this.CustomBar
		},
		methods: {
			returnPage(){
				console.log('=======this.currentStep', this.currentStep)
				if(this.currentStep>0){
					--this.currentStep;
				}else{
					uni.navigateBack()
				}
			}
		}
	}
</script>

<style>

	.page {
		/* margin: 20rpx;
		padding: 6rpx; */
		border-top: 1px solid #eee;
		margin-top: 14px;
		padding-top: 24px;
	}

	.statusBar {
		width: 100%;
		/* 		background-image:url("https://sxgp-innovate-1259195890.file.myqcloud.com/orderfood/of-wx-manager/index/indexBg_top.png");
		background-repeat:no-repeat;
		background-size:100% 100%; */
	}

	.coustomBarText {
		position: absolute;
		left: 50%;
		top: 50%;
		margin-left: -28px;
		margin-top: -8.5px;
		font-size: 14px;
	}

	.customBar {
		position: relative;
	}
</style>
  • swiper轮播

推荐使用uniapp自带的swiper:uni-app官网

一开始本来使用uview的swiper,发现自定义内容不太好展现。

  • 引入uview

uni-app系列(二):uniapp 项目引入 uView 并简单使用_Mr_chen的博客-CSDN博客_uniapp使用uview

  • 在内联样式中使用calc

  • 外部scss文件引入问题

uniapp官网示例引入css的方法

/* 绝对路径 */
@import url('/common/uni.css');
@import url('@/common/uni.css');
/* 相对路径 */
@import url('../../common/uni.css');

问题描述
刚开始按照示例引入common.scss文件,common.scss文件中的scss语法都不生效,比如变量被解析成字符串、嵌套写法的样式不生效等,实际表现类似css
正确的引入方法

@import '@/common/common.scss';
  • 小程序onLaunch和onload先后顺序

  • uniapp 滑太快到底部@scrolltolower没触发,再滑动一下触发2次

  • uni-app 时间格式问题 IOS系统跟Android系统不兼容

    //解决
    var newDate = new Date(date.replace(/-/g, ‘/’));

  • 中写position: fixed,在ios下会有兼容性问题

  • uniapp(Vue)修改对象或数据,页面没有相应更改
    原因:
    Vue 不允许在已经创建的实例上动态添加新的根级响应式属性, Vue 无法探测普通的新增属性

一、关于修改对象

Vue 无法检测 property 的添加或移除。由于 Vue 会在初始化实例时对 property 执行 getter/setter 转化,所以 property 必须在 data 对象上存在才能让 Vue 将它转换为响应式的。例如:

但是,可以使用 Vue.set(object, propertyName, value) 方法向嵌套对象添加响应式 property。例如

Vue.set(vm.someObject, 'b' ,2)

//或者
his.$set(this.someObject, 'b', 2)

二、关于修改数组

Vue 不能检测以下数组的变动:

可以使用 splice:
vm.items.splice(newLength)
  • props 传递对象,修改后不能使用问题

同上

  • 微信小程序 CSS border-radius元素 overflow:hidden失效问题 (iPhone ios 苹果兼容)

解决方法:
父元素设置以下两个属性:

backface-visibility: hidden;
transform: translate3d(0, 0, 0);
  • 小程序页面内含scroll-view时,在scroll-view组件外滑一下页面,会出现1-2秒卡死状态

解决:

方法一、页面设置position:fixed,固定
方法二、设置整体不能上下滚动
"disableScroll":true, ? ?//微信小程序
"mp-alipay": {
? ? "allowsBounceVertical":"NO" ? ?//支付宝小程序
}
  • 引入外部组件

    npm install moment

    安装之后,在页面,然后就和之前咱们在网页中是一样的了
    import moment from “moment”

  • navigateTo:fail can not navigateTo a tabbar page

跳转tabBar请用 switchTab(OBJECT),跳转到 tabBar 页面

//正常
uni.switchTab({
	url:'/pages/home/home'
})


//报错
// uni.navigateTo({
	// 	url: '/pages/home/home'
// })
  • 报错:navigate to:fail webview count limit exceed


redirectTo(Object object)
关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。

//一开始是这样写的
uni.navigateTo({
url: '/pages'
})
//改成这样写就OK
uni.redirectTo({
url: '/pages'
})

redirectTo:fail can not redirectTo a tabbar page

        //这一段报错
//uni.redirectTo({
			//url: '/pages/home/home'
		//})

//改成如下
		uni.switchTab({
			url: '/pages/home/home'
		})
  • 预览时报错:message:Error: 系统错误,错误码:80051,source size 3432KB exceed max limit 2MB

解决方法:分包加载

uniapp分包_前端小白成长记-CSDN博客

  • uni-forms内的值没有刷新

要点:v-model,v-if,等等,一般需要初始值,除非在前面加以判断

<uni-forms-item label="商品名称" name="name"
								class="goods-form-item form-item-border form-item-require">
								<input v-model="formData.name" type="text" placeholder="请输入商品名称" />
							</uni-forms-item>



下面一定要定义name初始值
data() {
			return {
				// 表单数据
				formData: {
					name: "",
				},
			}
		},
  • 微信小程序input type=“number” 键盘没有小数点

    <input class=“input” v-model=“price” type=“digit” placeholder=“请输入商品价格” @input=“onLimitPrice” maxlength=“20”/>

    methods: {

    		onLimitNumber(value) {
    			value = value.replace(/[^d.]/g,"");//清除"数字"和"."以外的字符
    			value = value.replace(/^./g,"");//验证第一个字符是数字而不是字符
    			value = value.replace(/.{2,}/g,".");//只保留第一个.清除多余的
    			value = value.replace(".","$#$").replace(/./g,"").replace("$#$",".");
    			value = value.replace(/^(-)*(d+).(dd).*$/,'$1$2.$3');//只能输入两个小数
    			//去除负号(限定输入正数)
    			if(value.indexOf('-')>=0){
    				value = value.replace(/-/g,'')
    			}
    			return value;
    		},
    		onLimitPrice(event) {
    			let value = event.detail.value;
    			let newValue = this.onLimitNumber(value)
    			setTimeout(() => { 
    				this.price = newValue
    				this.formData.sell_price = this.yuantofen(this.price)
    			}, 0)
    		},
    

    }

  • scroll-view 滚动到顶部

层次较浅的直接使用this.$set

层次较深的,必须先使用this. d e l e t e , 再 使 用 t h i s . delete,再使用this. delete使this.set才有效

this.$delete(this.selectGoodsGroups, 'goods')
this.$set(this.selectGoodsGroups, 'goods', optionList)

一个笨方法:

页面代码添加v-if='updateNewData'
<view class="item" v-for="(item1, index1) in rightScroll[index]" :key="index1" v-if='updateNewData'>
</view>


定义updateNewData: true,不然页面进来不会显示

data() {
			return {
				updateNewData: true,
			}
		},


// 在数据变化处添加如下两行代码
this.updateNewData = false;
this.updateNewData = true;
  • 隐藏scroll-view的滚动条

    /* 解决小程序和app滚动条的问题 */
    ::-webkit-scrollbar {
    display: none;
    width: 0 !important;
    height: 0 !important;
    -webkit-appearance: none;
    background: transparent;
    color: transparent;
    }

本代码参考了链接:uniapp scroll-view 隐藏滚动条的问题_miga1的博客-CSDN博客

  • uni-app watch无法监听到第一次绑定的变化

改成如下:

<input type="text" v-model="userName"/>  
watch: {
    userName: {
        handler (newName, oldName) {
            console.log(newName)
        },
        immediate: true
    }
}

本代码参考了链接:uni-app watch事件监听三种用法_张志翔的博客-CSDN博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于uni-app的适配和兼容问题,我可以给你一些建议。 首先,uni-app是基于Vue.js框架的跨平台开发框架,它可以同时开发小程序、H5、以及APP等多个平台。在适配方面,uni-app已经做了很多工作,使得开发者可以方便地编写一套代码,同时在不同平台上运行。但是仍然可能会遇到一些兼容性问题。 一些常见的适配和兼容问题包括: 1. 样式适配:不同平台的样式表现可能存在差异。你可以使用uni-app提供的`class`和`style`全局样式,根据不同平台设置不同的样式。 2. 组件适配:某些组件在小程序和H5平台上可能有不同的实现方式。你可以使用条件编译指令`<!-- #ifdef H5 -->`和`<!-- #ifndef H5 -->`来针对不同平台编写特定代码。 3. API适配:不同平台的API调用方式可能不同。你可以使用uni-app提供的内置API来实现跨平台兼容,或者使用条件编译指令来根据不同平台调用特定的API。 4. 图片适配:不同平台对图片格式和路径的要求可能不同。你可以使用uni-app提供的`@/static`路径来引用静态资源,并根据平台要求提供不同的格式。 5. 第三方插件兼容:某些第三方插件可能不支持所有平台。你可以在使用前检查插件文档,确认其支持的平台,并在不支持的平台上进行兼容处理。 总之,针对uni-app的适配和兼容问题,你可以通过使用uni-app提供的工具和特性,以及条件编译指令来进行处理。同时,建议在开发前了解各个平台的差异和要求,并在实际开发中进行测试和调试,以确保应用在不同平台上的正常运行。希望这些建议对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值