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

先看看错误的范例代码,当你单步调试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>

 

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用uni-app内置的uni.navigateTo方法跳转到系统浏览器打开知乎网页,可以按照以下步骤进行操作: 1. 在需要跳转的页面使用uni.navigateTo方法跳转到一个间页面(例如名为"externalLink"的页面)。 ```javascript uni.navigateTo({ url: '/pages/externalLink' }); ``` 2. 在"externalLink"页面的onLoad生命周期函数使用uni.getSystemInfo方法获取当前系统信息,并根据系统信息判断要打开的链接。 ```javascript onLoad() { uni.getSystemInfo({ success: (res) => { const platform = res.platform.toLowerCase(); let url = ''; if (platform === 'android') { url = 'intent://www.zhihu.com/#Intent;scheme=https;package=com.android.chrome;end'; } else if (platform === 'ios') { url = 'https://www.zhihu.com/'; } else { // 其他平台处理 } this.openExternalLink(url); } }); }, methods: { openExternalLink(url) { uni.redirectTo({ url: `/pages/webview?url=${encodeURIComponent(url)}` }); } } ``` 3. 在"externalLink"页面的methods,定义openExternalLink方法,并通过uni.redirectTo方法跳转到一个webview页面,并将要打开的链接作为参数传递给webview页面。 ```javascript openExternalLink(url) { uni.redirectTo({ url: `/pages/webview?url=${encodeURIComponent(url)}` }); } ``` 4. 创建一个webview页面(例如名为"webview"的页面),用于展示外部链接。 ```html <template> <view class="container"> <web-view :src="url"></web-view> </view> </template> <script> export default { data() { return { url: '' }; }, onLoad(options) { this.url = decodeURIComponent(options.url); } }; </script> ``` 在这个示例,通过判断当前系统平台(Android或iOS),选择不同的网页链接进行跳转。Android平台使用intent协议打开Chrome浏览器,iOS平台直接使用https协议打开知乎网页。 请注意,在跳转到webview页面时,需要将要打开的链接进行encodeURIComponent编码,以避免URL的特殊字符引起的问题。 以上是使用uni-app内置的方法跳转到系统浏览器打开知乎网页的示例代码。您可以根据实际需求进行适当调整和扩展。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值