惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 蓓蕾心晴

语雀思维导图如何导入到飞书文档 js 实现点击触发复制口令到剪贴板,并跳转 css 背景模糊在真机测试会出现黑色蒙层闪现问题解决 华为鸿蒙手机通过Chrome DevTools调试App内WebView页面 vscode左侧搜索栏搜索时排除不参与搜索的文件夹 css动画已经执行过一次如何再次执行? vscode设置单击选中带连字符的单词 移动端盒子元素实现左右可滑动且竖向页面可滑动 js 判断设备类型包括异形屏 element ui 日期组件实现仅显示日期选择但值包含固定的时间 master远端代码更新,本地拉取不到 css 实现刘海屏样式兼容并支持 js 获取刘海屏高度后动态修改 css 判断在支持某些属性的情况下再添加样式 vue3 provide的值 在回调函数中改变,inject 如何获取到最新的值? ResizeObserver loop completed with undelivered notifications. 报错 git 修改本地仓库的远程仓库地址 git突然无法推送到远程仓库 css实现图片等比例完全展示,背景加图片 200%放大虚化 element-ui 使用 el-date-picker 如何限制时间选择范围? element-ui 使用 el-date-picker 如何监听数据变更?
vue3如何将 app 全局变量对象变为响应式并监听到某个属性的改变
蓓蕾心晴 · 2024-10-08 · via 博客园 - 蓓蕾心晴

需求:

通过 getData() 方法获取到数据对象,想要在屏幕方向改变的时候,给获取到的对象设置  orientation,能实时修改

方案:

使用 reactive 包裹, template 直接使用全局变量下的属性,watch 直接监听对应属性

代码如下:

main.js
const config = reactive(getData())

    // 设置屏幕方向
    const setOrientation = () => {
      config.orientation = getOrientation()
      window.config.orientation = getOrientation()
    }
    setOrientation()
    document.addEventListener('DOMContentLoaded', setOrientation)
    // 绑定屏幕旋转事件
    window.addEventListener('resize', setOrientation)
    window.mraid.addEventListener('sizeChange', setOrientation)
    // 避免锁定屏幕方向的情况下横屏进入,获取方向不对
    setTimeout(setOrientation, 1000)

    app.config.globalProperties.$config = config
    app.provide('config', readonly(config))

*.vue   模板中直接使用

<Logo v-if="$config.orientation === 'portrait'"/>

监听config 某个属性

const config = inject('config')
// 监听屏幕方向的改变,改变后再调用初始化样式的方法
watch(() => config.orientation, init, { immediate: true })

const internalInstance = getCurrentInstance()
const { $config } = internalInstance.appContext.config.globalProperties
// 监听屏幕方向的改变,改变后再调用初始化样式的方法
watch(() => $config.orientation, init, { immediate: true })