uniapp中使用mixins控制横屏竖屏

概念

mixins概念和用法同vue在此不在赘述。

在根目录下创建mixins目录,在mixins目录下创建lockScreen.js

js 复制代码
export default {
	data() {
		return {};
	},
	onLoad() {
		// #ifndef H5
		plus.screen.lockOrientation('portrait-primary');
		// #endif
	},
	onShow: function() {},
	onUnload() {},
	methods: {
		// 设置屏幕方向为横屏
		setScreenAuto() {
			// #ifndef H5
			plus.screen.lockOrientation('auto');
			// #endif
		},
		// 设置屏幕方向为横屏
		setScreenLandscape() {
			// #ifndef H5
			plus.screen.lockOrientation('landscape-primary');
			// #endif
		},
		// 设置屏幕方向为竖屏
		setScreenPortrait() {
			// #ifndef H5
			plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		kaifazhong() {
			uni.showToast({
				title: "公测暂未开放",
				icon: "none"
			})
		}
	}
};

然后在main.js中引入mixins

js 复制代码
import lockScreen from '@/mixins/lockScreen.js'

// 注册全局混入
Vue.mixin(lockScreen)

在页面中使用

js 复制代码
// /pages/webview/webview
<template>
	<view>
		<web-view :src="webUrl" frameborder="0"
			style="position: absolute; left: 0px; top: 0; height: 100%;"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				webUrl: ''
			}
		},
		onLoad(params) {
			this.setScreenAuto();
			this.webUrl = params.url
		},
		onUnload() {
			this.setScreenPortrait()
		},
		methods: {
			
		}
	}
</script>

<style lang="scss" scoped>
::-webkit-scrollbar {
  width: 0;
  height: 0;
}
</style>
相关推荐
吴传逞2 天前
记一次uniapp微信小程序开发scss变量失效的问题
微信小程序·uni-app·scss
2501_915921432 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008892 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
!win !2 天前
uni-app项目Tabbar实现切换icon动效
小程序·uni-app
xw52 天前
uni-app项目Tabbar实现切换icon动效
前端·uni-app
2501_916007472 天前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview
豆豆(设计前端)2 天前
使用 Uni-app 打包 外链地址APK 及 iOS 注意事项
ios·uni-app
jingling5552 天前
uniapp | 解决组件样式不生效问题
前端·css·uni-app·html·学习方法
^Rocky2 天前
微信小程序(uniapp)实现连接蓝牙
微信小程序·uni-app·蓝牙连接
2501_915918413 天前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview