In business development, we often encounter the following situations: based on the interface data returned by the backend, the frontend saves it in the object Object. During the frontend development process, for the convenience of some scenarios, it is necessary to add corresponding attributes to the object, but these attributes are meaningless to the backend and we hope to delete them when saving and submitting. Real business code: The corresponding async saveData (type, data) { // Delete extra fields when submitting delete data.isCommonValue delete data.isRemoteValue await this.$request({ ...API.EDIT_SERVICE, method: type === 'add' ? 'post' : 'put', data }) } The above is a common way of writing, but it is not the best way in some scenarios and may bring some side effects. The following is explained by way of example: ExampleIn order to better demonstrate the above situation, we rewrite the example (only for the purpose of illustrating the implementation). let person = { id: '001', name: 'ligang', email: '[email protected]' } Request: When submitting to the backend, the email field needs to be deleted. Method 1: deleteSame as the business code processing method given above delete person.email console.log(person) // {id: '001', name: 'ligang'} The relevant attributes in the original data will also be deleted. Reflect.deleteProperty(person, 'email') Method 2: DeconstructionForm new objects, avoiding side effects where the original object is referenced. let {id, name} = person let newPerson = {id, name} console.log(newPerson) // {id: '001', name: 'ligang'} The reference to the original data will be cut off. This method is simple and easy to understand when there are few retained attributes; it is more complicated when there are too many retained attributes. let {email, ...newPerson} = person console.log(newPerson) // {id: '001', name: 'ligang'} The reference to the original data will be cut off. This method is simple and easy to understand when retaining a large number of attributes; it is more complicated when retaining too few attributes. ReplenishVue does not allow dynamically adding root-level responsive properties to an already created instance. The following methods are invalid! this.$set(this, 'email', '') this.$set(this.$data, 'email', '') Summarize In actual use, it is strongly recommended to use method 2 so as not to affect the original data. Especially in the mvvm framework, the original data is often responsive. data () { return { person: name: 'ligang', email: '[email protected]' } } }, methods: { deleteProp () { delete this.person.email // this.$delete(this.person, 'email') }, addProp () { this.person.email = 'xxx' this.$set(this.person, 'address', 'xxx') } } 1. Execute the 2. Perform the 1. 2. This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Three methods of automatically completing commands in MySQL database
>>: How to pass W3C validation?
Mysql multiple unrelated tables query data and pa...
Table of contents Install tinymce, tinymce ts, ti...
Table of contents Preface Common methods 1. Modif...
MySQL password is correct but cannot log in local...
The following code introduces MySQL to update som...
This article describes the linux system commands....
The format is simple: proxy_pass URL; The URL inc...
front end css3,filter can not only achieve the gr...
This article mainly introduces how to implement a...
Table of contents 1. Component bloat 2. Change th...
Problem Peeping In the server, assuming that the ...
Table of contents 1. Solution 2. Let the browser ...
This article example shares the specific code of ...
View the IP address of the Container docker inspe...
Example source code: https://codepen.io/shadeed/p...