javascript - nuxt layout - 为移动端和桌面端提供不同的布局

import { isMobile } from 'mobile-device-detect'

export default {
    layout(context) {
        const mobile = isMobile ? 'mobile-layout' : 'desktop-layout
        return mobile
    }
}

我正在创建一个 nuxt 应用程序。我正在尝试检测 View 何时是移动 View 或桌面 View 。如果我更改我的 ide 中的代码并保存,则 isMobile 为真,并且它将设置为移动布局。但是,一旦您刷新页面,isMobile 为 false,它将设置为桌面移动版。

我假设在它有时间确定 isMobile 是真还是假之前设置布局,并自动设置得太假。

我不知道如何让这样的东西发挥作用。无论我尝试什么,它总是失败。

最佳答案

当我们只有 Nuxt 社区为此目的提供官方支持时,为什么还要选择一些通用软件包?我建议您使用 @nuxtjs/device 模块。

它有什么特别之处?这个。 ?

This module injects flags that indicate a device type into the context and the component instance.

这就是动态更改布局的方法。它在包的文档中提到。 https://github.com/nuxt-community/device-module#nuxtjsdevice

export default {
    layout: (ctx) => ctx.isMobile ? 'mobile' : 'default'
}

https://stackoverflow.com/questions/60460660/

相关文章:

r - 在 Shiny 的服务器上增加堆栈维度

java - 使用 Jacoco Agent 远程触发 jacoco 执行数据转储

reactjs - 故事书:ReferenceError: Jest 未定义

imagemagick - 由于 magick_image_readpath 中的错误,无法将 ka

django - 发出命令时出现静态错误 "python manage.py collectstat

json - 将 GeoJSON 文件导入 React-Leaflet

android-studio - 如何禁用 Android-Studio 3.6 中引入的多预览功能

flutter - 是否可以通过 UDP/TCP 从 flutter 应用程序向 .NET 应用程序

javascript - 调用一次从 API 获取数据

reactjs - 清理本地存储后如何进行重定向?