A
A
Andrey2018-03-27 16:09:07
Vue.js
Andrey, 2018-03-27 16:09:07

How to properly pass JSON to vue js component?

From the site template to the component I pass data for initialization in json:

<div id="root">
            <my-component  docs='{{ json }}' />
        </div>

A string gets into the component, in the this.docs property, it must be parsed to get the object.
But it doesn't work like this:
const my-component = {
        props: ['docs'],
        data: function() {
            return {
                docs: JSON.parse(this.docs)
            };
        },
    };

You cannot change the incoming properties.
Of course, I can create a new property, like docs2 and just save the json in the docs property for memory, but I don’t like this idea :)
Where would it be more correct to parse the string?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-27
@f-end

<my-component :docs='{{ json }}' />
And no JSON.parse is needed, docs will be an object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question