M
M
Michael2020-07-24 15:03:15
Vue.js
Michael, 2020-07-24 15:03:15

Why does observation work so strangely?

Greetings.
There is a component:

<div id="app">
  <my-component> 
    </my-component>	
</div>


const MyComponent = {
  template: '<div>{{x}}<br>{{y}}</div>',
  data() {
    return {
      x: '',
      y: '',
      isOk: false
    }
    },
  created(){
      alert(this.isOk);
    this.x = 'x-first';
    this.y = 'y-first';
    alert(this.isOk);
    this.isOk = true;//теперь наблюдаем
  },
  watch: {
    x(){
      this.check();
    },
    y(){
      this.check();
    }
  },
  methods: {
    check(){
      alert('watch ' + this.isOk);
    }
  }
  };

In created, I set the initial values ​​to you.
But I also need to slash their changes, so I'm monitoring them.
But I see that when I assign values ​​to created, the observation already works for them.
The first question: How to act in such cases, how to turn off surveillance for a while?

I try to make a flag, as in the code above, by design, at first we don’t observe (isOk: false), but it doesn’t work that way, the output gives such alerts:
false
false
watch true
watch true

Those. The observer does not work immediately as you assign a value, but sometime later.
Second question: Why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-07-24
@Nolan81

https://ru.vuejs.org/v2/guide/reactivity.html#%D0%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question