VUE--Common components pass values and listen to data update views in time

Scenario: The two subcomponents of A and B are in the parent page, (our risk assessment information is A, and the filing information is B)

  A certain value of component B is the same as that of component A, and even if the value of component A changes, B listens and renders it. For example: in the figure below, component A of the total amount needs to pass the amount to the estimated loss amount box of component B, and when the amount of A changes, B also changes

       The amount has been bound in the A component

Use sibling transfer to transfer the amount of A component to the estimated loss amount of B component

1. The syntax of sibling components is. First build a bus.js file

import View from 'view'

export default new Vue()

2. Introduce bus.JS in A component

 3. Pass the local amount value to the B component. Here, it will be triggered when the value of the drop-down box changes.

 

 4. In order to update in time when the page is initialized, this method is called in created

 5. The B component imports the bus.js file and accepts the amount value passed by the A component when the page is initialized. (I use the local price value here to accept the passed in)

 

 6. At this time, the value passed by the A component has been received, and if there is a change in the monitoring value, the latest value is assigned to the local variable in time

 

Guess you like

Origin blog.csdn.net/weixin_62355341/article/details/123827578