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

标签 javascript vue.js nuxt.js

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'
}

关于javascript - nuxt layout - 为移动端和桌面端提供不同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60460660/

相关文章:

css - 如何在 Nuxt/Vue 中做内联线性渐变

javascript - Ember.js 组件 didRender 变量范围

javascript - datepicker onselect 不起作用 [当 datepicker div 动态创建时]

javascript - 如何延迟页面渲染直到刷新 vue 变量?

javascript - nuxtjs/axios 4.4.0版本的初始化方式是什么? (nuxt.js)

javascript - Nuxt.js 服务器端插件函数不是一个函数

Javascript parseFloat(number) - parseFloat(number2) 返回 int

javascript - 在脚本域的JS中设置cookie

javascript - Nuxt3 默认错误页面无法用 layouts/error.vue 覆盖

typescript - 带有 Typescript : missing RouteConfig 的 Vue3 路由器