在uniapp中设置屏幕方向为横向 landscape

600 阅读1分钟

今天使用uniapp开发微信小程序的时候,有这么一个需求,专门拿出一个页面用来展示echarts折线图,需求希望这个折线图可以在微信小程序上横向展示,我试了很多方法,比如使用plus在进入页面的时候设置方向,但是丝毫不起作用,也不报错。

onShow() { // #ifdef APP-PLUS plus.screen.lockOrientation("landscape"); // #endif },

后来使用 uni.setScreenOrientation,引发报错:common_vendor.index.setScreenOrientation is not a function

onMounted(() => { uni.setScreenOrientation({ orientation: 'landscape' }); isLandscape.value = true; }); onUnmounted(() => { uni.setScreenOrientation({ orientation: 'portrait' }); isLandscape.value = false; });

最后使用一个超级简单的方法解决的: 在 pages.json 中配置页面方向: "pageOrientation": "landscape"

{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",
        "pageOrientation": "auto"
      }
    },
    {
      "path": "pages/landscape-page/landscape-page",
      "style": {
        "navigationBarTitleText": "横屏页",
        "pageOrientation": "landscape"
      }
    }
  ]
}