Vue3——Chapter 4 (responsive basics: reactive, ref)


1. Declare responsive state with reactive()

  • We can create a reactive object or array using the reactive() function:
    insert image description here

  • Reactive objects are actually JavaScript Proxies, which behave like normal objects. The difference is that Vue is able to track access and changes to reactive object properties.

  • To use reactive state in component templates, it needs to be defined and returned in the setup() function.
    insert image description here

  • Naturally, we can also define a function that updates the reactive state in the same scope, and expose it as a method along with the state:
    insert image description here

  • Exposed methods are typically used as event listeners:
    insert image description here

Second, the limitations of reactive()

  1. Valid only for object types (object, array, and collection types like Map, Set), not primitive types like string, number, and boolean.

  2. Because Vue's reactive system is tracked through property access, we must always keep the same reference to the reactive object. This means that we cannot arbitrarily "replace" a reactive object, as this would cause the reactive connection to the original reference to be lost:
    insert image description here

  • It also means that when we assign or destructure a property of a reactive object to a local variable, or pass that property into a function, we lose reactivity:
    insert image description here

3. Use ref() to define responsive variables

  • The limitations of reactive() come down to the fact that JavaScript doesn't have a "reference" mechanism that works on all value types.
  • To this end, Vue provides a ref() method that allows us to create reactive refs that can use any value type:
    insert image description here
  • ref() wraps the value of the incoming parameter as a ref object with a .value attribute:
    insert image description here
  • Similar to the properties of a reactive object, the .value property of a ref is also reactive.
  • At the same time, when the value is an object type, its .value will be automatically converted with reactive().
  • A ref containing a value of type object can reactively replace the entire object:
    insert image description here
  • refs are passed to functions or destructured from regular objects without losing reactivity:
    insert image description here
  • In short, ref() allows us to create "references" to arbitrary values ​​and pass them around without losing responsiveness.

1. Unpacking ref in the template

  • When refs are accessed as top-level attributes in templates, they are automatically "unwrapped", so there is no need to use .value.
    insert image description here
  • 请注意, automatic "unwrapping" only applies when ref is a top-level property of the template rendering context. For example, foo is a top-level property, but object.foo is not.
    insert image description here

2. The unpacking of ref in the responsive object

  • When a ref is nested inside a reactive object, it is automatically unwrapped when accessed or changed as a property, so it behaves like a normal property:
    insert image description here

  • If you assign a new ref to a property associated with an existing ref, it will replace the old ref:
    insert image description here

3. Responsive syntactic sugar (experimental)

  • Compared with ordinary JavaScript variables, we have to use relatively cumbersome .value to get the value of ref.
  • This is a shortcoming limited by the limitations of the JavaScript language.
  • However, with a compile-time cast, we can let the compiler save us the trouble of using .value.
  • 请注意它仍处于实验性阶段,在最终提案落地前仍可能发生改动。
    insert image description here

Four,<script setup>

  • Exposing a lot of state and methods manually in the setup() function is tedious.
  • Fortunately, we can make this easier by using build tools.
  • When using single file components (SFC), we can use <script setup>to greatly simplify the code.
    insert image description here
  • <script setup>Imports and variable declarations at the top level in can be used directly in the same component's template.
  • You can understand that the expressions in the template and the code <script setup>in are in the same scope.

Guess you like

Origin blog.csdn.net/weixin_44733660/article/details/128628409