Get and set in the computed attribute in vue;

1.Computed is to monitor self-defined variables. The variables here are not defined in data, but directly defined in
computed. After the variables are placed on the display page, the two-way binding of data is realized. When variables When
a change occurs, the calculation will be triggered. When it comes to this, we need to talk about the second get and set methods.


2. There are get and set methods in computed (only get by default)


1. The get method is to fetch, which is equivalent to that we can assign a value to the variable in this computed property in get. 2. The set
method is triggered when it changes. The change here refers to the value of the variable defined in computed When the change occurs, the set method will be triggered, so that we can do
some things we want to do in the set method, (such as calling a certain method column:

this.$emit("input", newValue);

Pass the value to the parent component,

As we all know, props is a form of passing values ​​from parent to child. When the props value of our child component needs to be changed, the parent component cannot be changed directly by changing the props of the child component (otherwise an error will be reported).

solution

  • Use the computed method, which is the content introduced at the beginning
 computed: {
    count: {
      get() {
        return this.value;
      },
      set(newValue) {
        this.$emit("input", newValue);
      },
    },
  },

set()可以监听 参数value 值的变化

Guess you like

Origin blog.csdn.net/YZ_ZZZ24/article/details/124828079