Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

J_shelfwood's avatar

Vue component, how to access properties vs data

So I've started to work with Vue Js and I don't understand how the accessing of data and propeties works. I've got a very basic Vue component that fires a method when you press enter. Basically I want to send a post request with A: the value of the textfield (got that to work) and B: the value of a property I'm giving in the html tag (parentCategory) . I can't seem to access the parentCategory property. I know you would access it in the template like you would any data but when I console.log the value of the parentCategory It's undefined.

<script>
    export default
    {
        data() {
            return {
                category: {
                    name: '',
                    category_id: ''
                },
            showfield: false
            }
        },
        props() {
            ['parentCategory']
        },
        methods: {
            createCategory() {
                
                console.log(this.parentCategory)
                this.category.category_id = this.parentCategory
                console.log(this.category.category_id)
                this.$http.post('/categories', this.category).then((response) => {
                  console.log('received')
                }, (response) => {
                  console.log('failed to store')
                  //Remember that this might not fire if the backend failed.
                });
            }
        }
    }
</script>

What am I missing here? Is there a different way to access properties in component methods?

0 likes
6 replies
prasinoulhs's avatar

Change this

props() {
            ['parentCategory']
        },

with this

props: ['parentCategory'],
J_shelfwood's avatar

@prasinoulhs Just changed it, still when I console.log(this.parentCategory) it returns undefined. However, What I did find out is that apparently Vuejs doesnt like capital letters. this.parentcategory works.

When I inspect the element all letters of the property are lowercase. This means that using camelcase for your properties doesnt matter since they'll get converted to lowercase anyway when at the browser.

J_shelfwood's avatar

Ohh right so if I'd register a parentCategory prop, I'd use it on the custom tag like parent-category?

Please or to participate in this conversation.