Answer the question
In order to leave comments, you need to log in
Why is watcher not reactive when creating a component?
I used watcher to work with the search string and display a filtered list, everything works, but I decided to write the value of the search string in localStorage and the reload field, the value is substituted into the search string, but the watcher does not work and there is no reactivity, I tried to use immediate: true
, but it did not help:
const seachCityName = ref(localStorage.getItem("seachNameFromLS") || ""); // беру значение из localStorage
watch(
seachCityName,
() => {
localStorage.setItem("seachNameFromLS", seachCityName.value); // записываю значение из localStorage
// тут метод который фильтрует список по значению seachCityName из записывает в $store
},
{ immediate: true }
);
Answer the question
In order to leave comments, you need to log in
1. Everything works:
2. It's better to write like this:
watch(
seachCityName,
(value) => {
localStorage.setItem("seachNameFromLS", value);
// ...
},
{ immediate: true }
);
<code></code>
when asking a question. These are the norms of decency.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question