Communication between components (father to son)

 

A  parent -> Data transfer between the sub-assembly
 2  // parent component, the data transfer
 . 3  < Editor : inputIndex = "Data" : the inputName = "Wangwen Jian" > </ Editor > 
. 4  // subassembly, receive data transfer defined type type and a default value of the default data
 . 5      the props: {
 . 6          inputIndex: {
 . 7              type: Object, 
 . 8              default: function () {
 . 9                  return {}
 10              }
 . 11          },
 12 is          the inputName: {
 13 is              type: String,
 14              default: ' '
 15         }

Note entry:

 1. When the transfer of data between similar auxiliary write an attribute in the tag, data is transmitted if the data attributes will have to pass in front plus v-bind properties.

  If fixed values ​​are consistent: static string is passed directly without adding v-bind attribute in money;

       Number, Boolean, object. These expressions are js array instead of a string, the static even these also need to add v- bind, the data into the data referenced.

2. If the data transmitted prop subassemblies is an object, it is to be noted that pass an object reference, but all the same object, although seemingly disparate Sons assembly.

  If the value passed prop subassembly only used as an initial value, and does not change the assignment to the data used in the parent article.
  If the transmitted data prop subassembly is changed within parent components, the monitor changes the attribute calculation into use, since if the object is passed, then a property change only the following subassembly is not responsive update, if that sub-component needs updating or watch only put computed in a deep copy deep response when data changes: true to monitor changes.
  Of course, if you do need to change prop operation by transferring data in the sub-components, then write in the newspaper warning computed Dehui, because the calculation of property does not recommend any changes in the data, it's best carefully calculated. If you have to carry on operating data that can be written on the monitor watch (note deep deep copy) or computed using the get and set as shown:

 
  But again, if you pass ten objects come in, but at the same time you need to manipulate data passed in this sub-assembly, then the data in the parent component will change, because you pass just a reference, even if you copy data to the data in the prop is the same, anyway copy assignment is referenced, you can only do a deep copy of an object to create a copy of the order to proceed, you can use the method to convert a string of json then converted convenient target point.
  So when his son to pass data needs to consider how to use data books, otherwise you will encounter many problems or subassemblies modify the data between the father and son, it is very subtle and very dangerous.

Guess you like

Origin www.cnblogs.com/happyeveryuday/p/11840037.html