为何uni-app项目中使用uni.getSystemInfo获得的属性值绑定到动态样式上没有生效

本文详细解析了在uni-app项目中如何正确使用uni.getSystemInfo及uni.getSystemInfoSync来获取系统信息,并将其用于动态样式绑定,确保样式即时生效。通过对比错误与正确示例代码,帮助开发者掌握关键步骤。

先看看错误的范例代码,当你单步调试onLoad里面的代码时,能够输

出我们想要的结果,但这样的写法不能让正确的动态样式绑定生效:

<template>
	<view class="content" :style="{ height: wagesheight + 'px' }">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				wagesheight:0
			}
		},
		onLoad() {
			uni.getSystemInfo({
				success:function(res){
					console.log(res.windowHeight);// print 610
					this.wagesheight = uni.upx2px(res.windowHeight)
					console.log(this.wagesheight);// print 292
				}
			})

		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #4CD964;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

在我们使用uni.getSystemInfo获取系统属性值时,我们必须定义
一个函数,将uni.getSystemInfo包装到自定义函数中,将需要的属
性值 必须从自定义函数中返回,这样样式动态绑定就会立即生效,

正确的代码如下:

<template>
	<view class="content" :style="{ height: wagesheight + 'px' }" >
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area" >
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {
				title: 'Hello',
				wagesheight: 0
			}
		},
		onLoad() {
				this.wagesheight = this.getData()	
				console.log(this.wagesheight);
		},
		methods: {
		   getData(){
			var result = 0;
			uni.getSystemInfo({
					success: function(res) {
						console.log(res.windowHeight);// print 610
						result = uni.upx2px(res.windowHeight) + 200 // 这里加200或者加100为了看测试效果
						// 不加200默认 return 292
					}
			});
			return result;
		  }
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #4CD964;

	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

再列举一个在uni-app项目中使用uni.getSystemInfoSync()同步函数

获得的系统参数属性值绑定到动态样式上让动态样式生效的范例

<template>
	<view class="content" :style="{ height: wagesheight + 'px' }">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				wagesheight:0
			}
		},
		onReady() {
			/*
			// 这是错误获取windowHeight属性值的方式,因为此值没有被自定义的函数包装return返回,动态样式绑定没法生效
			uni.getSystemInfo({
				success:function(res){
					console.log(res.windowHeight);
					this.wagesheight = uni.upx2px(res.windowHeight) + 200
					console.log(this.wagesheight);// print 292px
				}
			})
			*/
		   
		    // 下面这行代码写到onLoad()里面动态样式绑定生效啦,uni.getSystemInfoSync()是同步方法
		    //this.wagesheight =  uni.upx2px(uni.getSystemInfoSync().windowHeight) + 200
		}
		,
		onLoad() {
			// 此行代码写到onReady()里面动态样式绑定生效啦,uni.getSystemInfoSync()是同步方法
            this.wagesheight =  uni.upx2px(uni.getSystemInfoSync().windowHeight) + 200
		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #4CD964;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>

 

uni-app中出现 `TypeError: requestTask.onChunkReceived is not a function` 错误,通常是因为使用了不支持的 API 或者 API 版本不兼容导致的。以下是一些可能的解决办法: ### 检查 API 兼容性 `requestTask.onChunkReceived` 这个 API 可能在某些平台或者版本中不被支持。需要确认当前使用uni-app 版本以及目标平台是否支持该 API。可以查阅 uni-app 的官方文档来确认 API 的兼容性。 ### 版本更新 确保 uni-app 及相关的插件、SDK 是最新版本。旧版本可能存在一些已知的 API 问题,更新到最新版本可能会解决兼容性问题。可以使用以下命令更新 uni-app 开发工具和相关依赖: ```bash npm update @dcloudio/uni-cli-i18n ``` ### 代码检查 检查代码中调用 `requestTask.onChunkReceived` 的部分,确认是否存在拼写错误或者使用不当的情况。例如,`requestTask` 对象可能没有正确初始化,或者在调用 `onChunkReceived` 之前已经被意外修改。 以下是一个示例代码,展示如何正确使用 `uni.request` 并处理请求: ```javascript const requestTask = uni.request({ url: 'https://example.com/api', success: (res) => { console.log('请求成功', res); }, fail: (err) => { console.error('请求失败', err); } }); // 这里要确保 requestTask 是有效的对象 if (requestTask && typeof requestTask.onChunkReceived === 'function') { requestTask.onChunkReceived((res) => { console.log('接收到数据块', res); }); } ``` ### 平台适配 如果该 API 在某些平台不支持,可以根据不同的平台进行适配处理。例如,在支持的平台使用 `onChunkReceived`,在不支持的平台采用其他方式处理数据。可以使用 `uni.getSystemInfo` 获取当前平台信息,然后进行条件判断: ```javascript uni.getSystemInfo({ success: (res) => { const platform = res.platform; if (platform === 'android' || platform === 'ios') { // 支持的平台,使用 onChunkReceived if (requestTask && typeof requestTask.onChunkReceived === 'function') { requestTask.onChunkReceived((res) => { console.log('接收到数据块', res); }); } } else { // 不支持的平台,采用其他处理方式 console.log('当前平台不支持 onChunkReceived'); } } }); ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值