[Vuejs]-I got this error TypeError: Cannot read property ‘url’ of null

0👍

The comments give you some idea to fix the issue. and here is why:

when the page/component loading, if you are using data to render the template, it will render the template with the init data you defined, that’s null for posts in your code.

to fix this, either you put some default data for your data prop in your Vue component. like

data() {
            return {
                posts:[{image:{url: "../assets/logo.png"}}],

            }
        }

or using if or ?: in the template.

I personally like my way, this way lets you know what’s your data struct is. make your code clear and easy to debug.

Leave a comment