[英]'Cannot read property 'color1' of undefined' error in vue.js
I am making a website with storyblok, vue and nuxt. 我正在建立一个包含Storyblok,Vue和Nuxt的网站。 In my component, I am trying to change the background color of the component by getting a value out of the storyblok API and getting the right HEX code in my object. 在我的组件中,我试图通过从Storyblok API中获取值并在对象中获取正确的十六进制代码来更改组件的背景颜色。 However, I can't seem to find what is going wrong, I am getting this error. 但是,我似乎找不到问题出在哪里,我得到了这个错误。
TypeError: Cannot read property 'color1' of undefined
at VueComponent.backgroundColor (app.js:6239)
at Watcher.get (commons.app.js:25459)
at Watcher.evaluate (commons.app.js:25564)
at VueComponent.computedGetter [as backgroundColor] (commons.app.js:25814)
at Object.get (commons.app.js:23107)
at Proxy.render (app.db592a6f79c830fedfca.hot-update.js:55)
at VueComponent.Vue._render (commons.app.js:24557)
at VueComponent.updateComponent (commons.app.js:25048)
at Watcher.get (commons.app.js:25459)
at Watcher.run (commons.app.js:25534)
I would really appreciate if someone could help me with this error. 如果有人可以帮助我解决此错误,我将不胜感激。 This is my code: 这是我的代码:
The div I am trying to change the background color of: 我正在尝试更改div的背景颜色:
<div class="column image" :style="{ backgroundColor: backgroundColor}">
<div class="image" :style="{ paddingTop: getRatio }">
<img class="lazyload" :src="blok.image"/>
</div>
</div>
My object in the data() function: 我在data()函数中的对象:
methods: {
data()
{
return {
colors: {
color1: '#ffffff',
color2: '#5646F2',
color3: '#000000'
}
}
}
}
My computed function: 我的计算函数:
backgroundColor() {
return !this.blok.color ? this.colors.color1 : this.colors[this.blok.color];
}
this.colors
is undefined because your data()
is inside the methods
hook. 由于您的data()
位于methods
挂钩内,因此this.colors
是未定义的。 Define the data
hook outside the methods
hook. 在methods
挂钩之外定义data
挂钩。
When you look for this.colors
, it will look for the property colors
defined inside the data
hook. 当您寻找this.colors
,它将寻找在data
挂钩内定义的属性colors
。 And when you look for a method for eg. 而当您寻找例如的方法。 this.myMethod()
, it will look for the method inside the methods
or computed
hook as necessary. this.myMethod()
它会寻找内部的方法methods
或computed
钩是必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.