【发布时间】:2021-07-03 05:22:03
【问题描述】:
我有一个 vue 应用程序,它具有可以更改主题的功能,例如,light/dark,
最初,我设置了我的localStorage.setItem['app_theme', 'light'],
我在Header.vue 组件中有我的应用主题更换器功能,点击主题更换器切换按钮后,它也会更改localStorage['app_theme'] = 'dark'。
现在,在我的其他组件中,我有一些像这样的computed values/variables/properties:
...
computed() {
app_card() {
return localStorage.getItem('app_theme') === 'dark' ? 'card-dark' : 'card-light'; //these are some classes with their respective theme css
},
app_text() {
return localStorage.getItem('app_theme') === 'dark' ? 'text-dark' : 'text-light'; //these are some classes with their respective theme css
}
}
...
我曾考虑使用轮询每 2 秒获取一次 localStorage.getItem('app_theme') 值,但我认为这会降低我的应用程序的速度。
是否有其他替代方法可以在不轮询的情况下侦听本地存储项更改?
【问题讨论】:
标签: vue.js local-storage computed-properties