[Vuejs]-Bind dynamically to this vuejs

0👍

It’s not completely clear to me what you want to accomplish, but I think you’re asking about creating a dynamic data property for a view component. If that’s the case, there are a couple of things to consider.

First, the example you cite, this.fieldName is not correct JavaScript syntax if fieldName is a string that contains a property name. The correct version is this[fieldName].

Note, though, that you can’t simply define a new data property for a Vue component by setting it to a value. That’s a limitation of JavaScript that’s described in the Vue documentation. If data[fieldName] is an existing property that’s defined in the component’s data object, then you’ll be okay. Even if you don’t know the value of the property, you can initialize it, for example, with a value of null and then update the value in your method. Otherwise, you’ll need to add the property to an existing non-root-level property as the documentation explains.

Leave a comment