Get the App
SLTechnology News&Howtos  ›  Development  › 

How does vue empty an array

Shulou Source: shulou.com Published: 2022-06-02 04:55:31 10月13日 Update

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!

Tags: Array mode article content situation scheme problem simplicity as the name implies not much not personal function preface side effects at the same time most objects guys buddies. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple Shulou Tech Info Shulou Information Microsoft