简体   繁体   English

vue.js中的'无法读取未定义的属性'color1''错误

[英]'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()它会寻找内部的方法methodscomputed钩是必要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法读取vue.js中“未定义”的属性 - Cannot read property of 'Undefined' in vue.js 错误 Vue.js“无法读取未定义的属性‘props’” - Error Vue.js "Cannot read property 'props' of undefined" Vue.js 计算属性:[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘userId’” - Vue.js computed property : [Vue warn]: Error in render: "TypeError: Cannot read property 'userId' of undefined" vue.js:属性或方法“”未定义,渲染错误:“TypeError:无法读取未定义的属性” - vue.js : Property or method "" is not defined, Error in render: "TypeError: Cannot read property '' of undefined" TypeError:无法读取未定义的“已创建”属性,未定义错误“Vue” Vue.js 3 - TypeError: Cannot read property 'created' of undefined, error 'Vue' is not defined Vue.js 3 “类型错误:无法读取未定义的属性‘服务’”Vue.js + Vuefire - "TypeError: Cannot read property 'servicios' of undefined" Vue.js + Vuefire TypeError:无法读取未定义的属性“标题”。 Vue.js - TypeError: Cannot read property 'title' of undefined. Vue.js 无法使用 vue.js 和 axios 读取未定义的属性“帖子” - Cannot read property 'post' of undefined using vue.js and axios Vue.js 无法读取未定义的属性“集合” - Vue.js Cannot read property 'collection' of undefined Vue.js 1.0.15过滤列表无法读取未定义的属性 - Vue.js 1.0.15 filtering lists cannot read property of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM