In vue achieve data transfer between the components by using the $ attrs

  Way of passing data between components, there are many, the reason why there are so many ways, in order to meet different scenarios under different conditions.
  
  There are three general ways:
  
  by way of props to transfer subassembly (Sons assembly)
  
  for vuex state management
  
  communication component transmitting non Sons Vue Event Bus, Example Vue achieve and monitor events published to accomplish the transfer between the components
  
  described herein introduce the use of $ attrs way.
  
  This api is added in version 2.4, so why do you want to add this feature?
  
  Take a look at the official website is how to explain
  
  included parent scope is not recognized as a prop (and get) the characteristics of the binding (except for class and style).
  
  When a component does not declare any prop, where the parent scope will include all of the binding (and except for class style),
  
  and can be v-bind = "$ attrs" Incoming internal components - when you create a high-level components very useful.
  
  The first to see if it is not easy to understand, this is both a usage and explains why the use of obscure, or I'll explain it.
  
  Meaning: $ attrs can collect all pass over the property of the parent components in addition to those defined by the component's no props.
  
  Extended explain, if the nested hierarchy of components a little dark but not so deep, such as three.
  
  If we use props, then, most of which components you want to get the data component of the outermost layer, it is necessary to pass through the props in the middle of the components,
  
  but the props for the middle of this component nothing useful, it is a bridge made of it . We usually write the code, when in fact often encounter
  
  This scene, to write sometimes feel annoying. So there is this $ attrs to help us, do not have to write props can make in the middle of the components in
  
  the innermost component to get the most out of the data passed in the assembly.
  
  So, specifically how to use it?
  
  Consider the following code in it, very simple to understand the
  
  preparation of three components
  
  inside the code below
  
  // grandfather
  
  <Template>
  
  <div style = "background: Blue">
  
  Father grandfather in
  
  <Father: Father-Age = "50": = Time-Child "` `$} {Time"> </ Father>
  
  </ div>
  
  </ Template>
  
  <Script>
  
  Import from Father './father'
  
  Export default {
  
  Components: {
  
  Father
  
  },
  
  Data () {
  
  return {
  
  Time:. new new a Date () the getTime ()
  
  }
  
  }
  
  }
  
  </ Script>
  

  

  
  <div style="background: red">
  
  child in father
  
  <div>
  
  <span>father age:</span>{{fatherAge}}
  
  </div>
  
  <child v-bind="$attrs"></child>
  
  </div>
  
  </template>
  
  <script>
  
  import child from './child'
  
  export default {
  
  components: {
  
  child
  
  },
  
  props: {
  
  fatherAge: {
  
  type: Number,
  
  default: 0
  
  }
  
  }
  
  }
  
  </script>
  
  //child<template>
  
  <div style="background: green">
  
  <div>child</div>
  
  <div>time: {{childTime}}</div>
  
  </div>
  
  </template>
  
  <script>
  
  export default {
  
  props: {
  
  childTime: {
  
  type: String,
  
  default: ''
  
  }
  
  }
  
  }
  
  </ Script>
  
  need to pass the data directly from the grandfather subassembly components, not declared in the parent assembly props.
  
  On the v-bind subassembly by way of the parent components you may not need to obtain from the subassembly assembly grandfather declared data to the subassembly.
  
  Of course, sub-assemblies props must be to declare, or props usage friends.
  
  The last thing to note is, $ attrs is to tie inheritAttrs: false use Oh, you can see why the specific Xiaguan network tutorial https://cn.vuejs.org/v2/guide/components-props.html#%E9%9D % 9E-Prop-% E7% 9A % 84% E7% 89% B9% E6% 80% A7 disable feature tutorials this section, but in fact talking about is not clear, it is recommended google it.
  
  Nodes type = const [Val] .Type;
  
  IF (type === 'a') {
  
  // If the type is a, then stored in the array nodeTypeIsA, after the determination of whether the length of the array nodeTypeIsA 0
  
  // 0 if not it indicates this level there is a node type, the total number may be increased by one, and then remove all child nodes of a type of the
  
  // next node calculation, until the hierarchy is not a type of node has been
  
  nodeTypeIsA. push (val);
  
  const n = nodes[val].childNodeIds ? Object.keys(nodes[val].childNodeIds) : [];
  
  childNodeIds = [...childNodeIds, ...n];
  
  }
  
  });
  
  if (nodeTypeIsA.length === 0) {
  
  return total;
  
  }
  
  ++total;
  
  return statisticalNextNodeNumber(childNodeIds, total);
  
  }
  
  function statisticalBeforeNodeNumber(nodeIds, num) {
  
  let total = num;
  
  let nodeTypeIsA = [];
  
  let beforeNodeIds = [];
  
  const nodes = nodes; // 获取所有节点信息的方法或对象
  
  nodeIds.forEach((val) => {
  
  const type = nodes[val].type;
  
  if (type === 'a') {
  
  nodeTypeIsA.push(val);
  
  const n = nodes[val].parentNodeIds ? Object.keys(nodes[val].parentNodeIds) : [];
  
  = beforeNodeIds [... beforeNodeIds, ... n-];
  
  }
  
  });
  
  IF (nodeTypeIsA.length === 0) {
  
  return Total;
  
  }
  
  ++ Total;
  
  return statisticalNextNodeNumber (beforeNodeIds, Total);
  
  }
  
  copy the code
  
  above two the most important functions idea is to follow the hierarchy to find, only the hierarchy there is a type, then the number of plus 1, and subsequent calculations only on the type of a node as a child node of. So you can find out when the two connected nodes adjacent node type is a maximum number of how many.
  
  These are the sharing of content, we want to help.
  
  Such as data bus 32 bits, it can fetch 4 bytes 4 bytes manner. 0-3,4-7,8-11,12-15, ...... even if the data we need only one byte, but also a read 4 bytes. One byte of data no matter what address that can be read out by a memory access. If the data is to be read more than one byte, two bytes for example, if the memory address of the data is 0x03, you need to read the data twice, first read 0x00-0x03, second reading 0x04-0x07. The data on the memory access across the border. The relative CPU computing, the memory access is very slow, so try to reduce the number of memory accesses. In order to reduce the overhead of memory access memory access data across borders cause, so the compiler will perform memory alignment, that is, the address of the variable to do some offset, the purpose of a memory access to read data, but also with as little as possible otherwise read data memory access times. As integral members of the address i is one example as to make a 4-byte offset, and the address is aligned Sample objects will do 4-byte boundary, so that the address i is always a multiple of 4, so that i is not memory access across the border, to read out its value again.
  
  <Dependencies>
  
  <-! ############### Date Time Operating ################ ->
  
  <dependency>
  
  <the groupId> Time-Joda </ the groupId>
  
  <the artifactId> Joda-Time </ the artifactId>
  
  </ dependency>
  
  <dependency>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> Starter-Spring-Boot-Data-MongoDB <
  

  
  <dependency>
  
  <groupId>cn.hutool</groupId>
  
  <artifactId>hutool-all</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter<www.xintiandiyule1.com /artifactId>
  
  </dependency>
  
  <!--###############springboot-aop模块################-->
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-aop</artifactId>
  
  </dependency>
  
  <!--###############test模块################-->
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-test</artifactId>
  
  <scope>test</scope>
  
  </dependency>
  
  <!--###############web模块################-->
  
  <dependency>
  
  <groupId>org.www.haishenggw.com springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-www.zheshengyule.com web</artifactId>
  
  </dependency>
  
  <!--###############lombok################-->
  
  <dependency>
  
  <groupId>org.projectlombok</groupId>
  
  <artifactId>lombok</artifactId>
  
  </dependency>
  
  <!--fast json-->
  
  <dependency>
  
  <groupId>com.alibaba<www.hongyaoyuL.cn /groupId>
  
  <artifactId>fastjson</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>com.google.guava</groupId>
  
  <artifactId>guava<www.jintianxuesha.com /artifactId>
  
  </dependency>
  
  </dependencies>
  
  基于maven dependencyManagement 版本控制如下:
  
  <dependencyManagement>
  
  <dependencies>
  
  <dependency>
  
  <groupId>org.springframework.boot<www.fengmingpt.com /groupId>
  
  <artifactId>spring-boot-dependencies</artifactId>
  
  <version>2.1.2.RELEASE</version>
  
  <type>pom</type>
  
  <scope>import</scope>
  
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  
  <dependency>
  
  <groupId>mysql</groupId>
  
  <artifactId>mysql-connector-java</artifactId>
  
  <version>5.1.48</version>
  
  </dependency>
  
  <dependency>
  
  <groupId>cn.hutool</groupId>
  
  <artifactId>hutool-all<www.fengminpt.cn /artifactId>
  
  <version>4.5.16</version>
  
  </dependency>
  
  <!--mybatis-->
  
  <dependency>
  
  <groupId>org.mybatis.www.jiuhaoyulept.com spring.boot</groupId>
  
  <artifactId>mybatis-spring-boot-starter</artifactId>
  
  <version>2.0.0</version>
  
  </dependency>
  
  <!--fast json-->
  
  <dependency>
  
  <groupId>com.alibaba</groupId>
  
  <artifactId>fastjson</artifactId>
  
  <version>1.2.56</version>
  
  </dependency>
  
  <!-- druid -->
  
  <dependency>
  
  <groupId>com.alibaba</groupId>
  
  <artifactId>druid-spring-boot-starter</artifactId>
  
  <version>1.1.9</version>
  
  </dependency>
  
  <dependency>
  
  <the groupId> com.google.guava </ the groupId>
  
  <the artifactId> Guava <www.jiuhaozhuce.cn / the artifactId>
  
  <Version> 19.0 </ Version>
  
  </ dependency>
  
  </ Dependencies>
  
  </ the dependencyManagement>
  
  Sample1 accounted how much space? Still is 8 bytes. At a byte 0, b is in the first byte, i accounts for 4-7 bytes. This is the principle of memory alignment, taking up as little memory. If after b, and c and char type of member d, the same is 8 bytes long. a, b, c, d in 0-3 bytes.

Guess you like

Origin www.cnblogs.com/dakunqq/p/11443718.html