$ Set and array update Methods in vue. js, and vue. jsset array update

Source: Internet
Author: User

$ Set and array update Methods in vue. js, and vue. jsset array update

Due to JavaScript restrictions, Vue cannot detect the following changed Arrays:

When an array item is directly set using indexes, such as vm. items [indexOfItem] = newValue

When you modify the length of an array, such as vm. items. length = newLength, the array is not updated.

Of course, the solution provided in vue is to use Vue. set, vm. $ set (Vue. modify the array, such as splice and caoncat, and trigger status update:

Ex:

Therefore, if you add a new property to the instance after the instance is created, the update is not triggered.

Ps:Now there are two Arrays: arr1 and arr2. If the values below arr1 change the array, what is the result of changing the array with $ set?

Data: {arr1 = ['A', 'B', 'C']; arr2 = ['foo', 'bar', 'baz'];} vm. arr1 [1] = 'alpha'; vm. $ set (vm. arr2, 1, 'alpha ');

Changing the array according to the subscript index at the beginning cannot trigger status update. We will know that the second change of the first array will not be updated on the page, only changes to the second array will be updated on the page. However, the result is:

Arr1 = ['A', 'alpha', 'B', 'C']; arr2 = ['foo', 'alpha', 'bar', 'baz'];

The values of the two arrays are updated. That is to say, when arr2 is updated using the $ set () method, the page is updated again.

In the above vue. js, $ set and array update methods are all the content shared by xiaobian. I hope to give you a reference, and I hope you can provide more support for the customer's house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.