Sign in

Doubts about the use of props in Vue

curumim edited in Mon, 20 Jun 2022

Article.vue Is the parent component, ArticleComments.vue It's a subcomponent that posts comments and displays a list of comments. The article ID is required to get the comment list of article content and publish comments. The article ID is stored in the vuex state. Which of the following two ways is better

  1. After the parent component obtains the article content and comment list, it passes in the child component through props, and the article ID and comment method also pass in the child component through props
  2. Instead of props, the subcomponents request by themselves. The required methods are defined in the subcomponents, and the article ID is directly passed through the store . Get state
4 Replies
commented on Mon, 20 Jun 2022

The second method is recommended to reduce the coupling between components. In addition, it is recommended to put the requested functions in a unified directory, which helps to separate the model layer from the view layer

commented on Mon, 20 Jun 2022

Passing through props will increase the coupling of components. Components should be independent. Since the ID exists in the store, it should be retrieved from the store. There is no need to retrieve it from the parent component and pass it to the child component.

commented on Mon, 20 Jun 2022

Let's use the second one. I was the first one before, but the background requests the data subcomponent not to update the view

commented on Mon, 20 Jun 2022

Why does this kind of data need to be stored in vuex and shared globally?

lock This question has been locked and the reply function has been disabled.