Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the disadvantage of using this.$root in vue components?

In vue's official docs I found the usage about $root at the edge cases section. "Edge Cases" makes me feel like using this.$root(or this.$parent) to mutate the data of root instance in a child component is not a normal or recommended way.

I know vuex is the best state management option for large and complex vue application and it has more advanced features and better for debugging. But that does not convince me the this.$root is not good.

As the docs says when this.$root to mutate root data is not possible to track when and where data changes happen and is not good for debugging. But what I want to know is:

Is this.$root approach only has disadvantages about debugging? Except for debugging related problems, is there any other issue of using this.$root to mutate the root data? If so can anyone give me a small example to showcase that problem because I cannot think out any case to avoid using it (suppose my vue app is not that large and complex). Thanks in advance!

like image 312
STEN Avatar asked Jul 19 '19 04:07

STEN


People also ask

What is $parent in VueJS?

How $parent is described in Vue? The $parent property, like $root, can be used for accessing the parent instance from a child. It provides direct access, making the application hard to test and debug.

What are the 3 parts of a component in Vue?

Components in Vue are composed of three parts; a template (which is like HTML), styles and JavaScript. These can be split into multiple files or the same .

What is createApp in VueJS?

The createApp API allows multiple Vue applications to co-exist on the same page, each with its own scope for configuration and global assets: js const app1 = createApp({ /* ... */ }) app1.

Why should I use Vue instead of HTML?

It's common for an app to be organized into a tree of nested components: This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate custom content and logic in each component. Vue also plays nicely with native Web Components.

Where is the root component in Vue?

In Vue CLI, the root component is just like any other component and is at src/App.vue. To create a component in Vue, we start with an empty object, just like the root component. But, instead of passing it to the Vue.createApp() function, we register it to the component we want to use it in.

What are the disadvantages of Vue JS?

Despite the advantages of Vue.js, there are definitely a few drawbacks that need to be addressed. For instance, stability remains an issue since the release of the first version, back in 2015.

What is component-based approach of Vue?

The component-based approach of Vue enables the formation of reusable single file components. Inside a component, template, logic, and styles are inherently coupled. Instead of separating the code into arbitrary layers, Vue collates components that can be reused and into a function. This makes the UI component cohesive, portable and maintainable.


1 Answers

This is all about good and bad architecture. You should always design your code so you share the least data you can (this is not even related to Vue).

Your vue component has private and public methods and fields. Consider all the method in component to be private. And $emit and props to be public. Accessing private methods and fields are always a bad idea, why?:

  • It's not clear who changed the state. If you have something like @click some deep child, you could end up in the whole tree of components rerendering because you might wanna change data inside of root.
  • How would you unit test your component? You will need to attach the root all the time.
  • Ok, imagine then that you have a huge enterprise application. And you access $root in multiple places. Now a new developer comes and changes the call signature withing one of a component but forgets to change it from another one. You will end up in a broken app. How is it different from vuex? In your vuex you should have modules, so there won't be a single place that all components access.
  • So debugging. Tools like vue-devtools track vuex but not state. Now imagine that some data just magically change in your component. And this is because another developer did something like setInterval(() => $root.setDataTime(Current time ${Date.now()}), 1000). You check the changes and there're no commits in the vuex nor on your component.
like image 104
deathangel908 Avatar answered Nov 15 '22 00:11

deathangel908