Issue at Hand
[Vue warn]: Property or method "changeSetting" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in <MainTable>)
The error stems from the fact that the changeSetting
method is being referenced in the MainTable
component as shown below:
"<button @click='changeSetting(index)'> Info </button>" +
However, the changeSetting
method is defined in the root component, not in the MainTable
component:
var app = new Vue({
el: "#settings",
data: data,
methods: {
changeSetting: function(index) {
data.settingsSelected = data.settings[index];
}
}
});
It's crucial to remember that properties and methods can only be referenced in the scope where they are defined.
Parent template content is compiled in parent scope; child template content is compiled in child scope.
For more information on Vue's component compilation scope, refer to the documentation.
Solution Strategies
To address this issue, consider relocating the changeSetting
definition into the MainTable
component.
While this may seem straightforward, an optimal approach would involve making your MainTable
component a dumb/presentational component, leaving the logic to the smart/container element. With this setup, you can use Vue's parent-child communication techniques for component interaction. Data is passed down to MainTable
via props and user actions are emitted from MainTable
to its parent using events. Here's a sample implementation:
Vue.component('main-table', {
template: "<ul>" +
"<li v-for='(set, index) in settings'>" +
"{{index}}) " +
"{{set.title}}" +
"<button @click='changeSetting(index)'> Info </button>" +
"</li>" +
"</ul>",
props: ['settings'],
methods: {
changeSetting(value) {
this.$emit('change', value);
},
},
});
var app = new Vue({
el: '#settings',
template: '<main-table :settings="data.settings" @change="changeSetting"></main-table>',
data: data,
methods: {
changeSetting(value) {
// Handle changeSetting
},
},
}),
By following the above example, you should be able to navigate towards a solution and start resolving the encountered issue effectively.