Rule Definition
The point of components is to be small, dynamic, independent and usable anywhere - a stand-alone piece of logic, that receives something, does something and returns something. Binding it to a direct parent destroys the concept of using the component anywhere.
In most cases, reaching into the parent makes the application more difficult to debug and understand, especially if parent data is mutated. When looking at that component later, it will be very difficult to figure out where that mutation came from.
This is how you keep the dataflow clean and without implicit code.
Remediation
Use custom events and $emit to send emissions to the parent which vue will detect
Violation Code Sample
export default {
props: {
isSelected: {
type: Boolean,
required: true
}
},
methods: {
toggle(){
this.$parent.isSelected = !this.$parent.isSelected
}
}
})
Fixed Code Sample
export default {
props: {
isSelected: {
type: Boolean,
required: true
}
},
methods: {
toggle(){
this.$emit('input', !this.value)
}
}
})
// in parent
<my-component v-model = "value"/>
<my-component :value="value" @input= "(newVal) => {value = newVal}" /> // update on input event
Reference
https://vuejs.org/v2/guide/components-edge-cases.html#Accessing-the-Parent-Component-Instance
https://vuejs.org/v2/guide/components-edge-cases.html#Accessing-the-Root-Instance
Related Technologies
Technical Criterion
CWE-1047 - Modules with Circular Dependencies
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.