【发布时间】:2020-04-18 00:10:26
【问题描述】:
我正在尝试从 nuxt js 中的 locastorage 读取数据。
我使用了 process.browser if 语句。 没有错误。
这是我的代码:
data() {
return {
classes: ['light', 'dark', 'darker'],
current: 0
}
},
created() {
if (process.browser) this.getBgDarkness()
console.log(this.classes[this.current])
},
methods: {
makeBgDarker() {
this.current < 2 ? (this.current += 1) : (this.current = 0)
if (process.browser)
window.localStorage.setItem('darknessLevel', this.current)
},
getBgDarkness() {
this.current = Number(window.localStorage.getItem('darknessLevel'))
}
}
这是我的模板:
<header :class="['header position-relative', `header--${classes[current]}`]"></header>
localstorage 设置成功,我的current 数据同步到它。
但我的 css 类没有改变。
这是我的控制台: console screenshot
【问题讨论】:
标签: vue.js vuejs2 local-storage nuxt.js