In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you vue how to empty the array, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Preface
Two days ago, I encountered a problem at work: how to empty the responsive array generated by reactive in vue3. Of course, this is what I usually write:
Let array = [1, 2, 3]; array = []
However, there is still a problem with the way it is used in the reactive proxy, such as this:
Let array = reactive ([1 array 2)]); watch () = > [... array], () = > {console.log (array);},) array = reactive ([])
Obviously, because the reference to the original responsive object is lost, the monitoring is lost directly.
two。 Several ways of emptying data
Of course, as a decade code experience, I immediately came up with several solutions.
2.1 use ref ()
With ref, this is the easiest way to:
Const array = ref ([1Jing 2jue 3]); watch (array, () = > {console.log (array.value);},) array.value = []
2.2 using slice
Slice, as its name implies, is to slice an array and return a new array, which feels a bit like slicing in go. Of course, friends who have used react should often use slice. To empty an array, you only need to write:
Const array = ref; watch (array, () = > {console.log (array.value);},) array.value = array.value.slice (0mem0)
However, you need to be careful to use ref.
2.3 length assigned to 0
Personally, I prefer this, and directly assign length to 0:
Const array = ref ([1Jing 2jue 3]); watch (array, () = > {console.log (array.value);}, {deep:true}) array.value.length = 0
Also, this will only trigger once, but you need to note that watch needs to turn on deep:
However, in this way, it is more convenient to use reactive without having to open deep:
Const array = reactive ([1 array 2]); watch (() = > [... array], () = > {console.log (array);}) array.length = 0
2.4 using splice
The side effect function splice is also a solution, in which case you can also use reactive:
Const array = reactive ([1J2 array 3]); watch (() = > [... array], () = > {console.log (array);},) array.splice (0journal array.length)
Note, however, that watch triggers multiple times:
Of course, you can also use ref, but note that in this case, you need to turn on deep:
Const array = ref, watch (array, () = > {console.log (array.value);}, {deep:true}) array.value.splice (0meme array.value.length)
But you can see that ref, like reactive, triggers multiple times.
The above is all the content of the article "how to empty the array by vue". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.