当前位置:网站首页>Method of changing object properties
Method of changing object properties
2022-07-06 02:13:00 【MonsterQy】
List of articles
background
When completing form validation , Some data attribute names returned from the backend that need remote search , When validating with the last submitted form , The required attribute names are inconsistent ,Eg: The product information returned by the backend is producet_name,product_id
, But what is needed in the end is one product object , It contains id and name Two attributes .
The situation paves the way
Before formally introducing the solution , The author first introduces it in more detail , The background of the project , In order to better understand the solution . The project USES element-plus, This happens in , utilize el-select Components , Link remote search , Get some data of the database directly , These data are in the format of objects , For reference at this time element-plus Documents , You can know , When el-option Data in object format , Yes el-select Set up value-key attribute , Attribute value corresponding el-options Of key value ( Unique ), such , When clicking on an option , Information about the object format of this option , Save to v-model The value of the binding .
resolvent
For this problem , There are two solutions :
1. stay submit when , Revise the attribute field key name required by the form , This requires the child component to modify the properties of the parent component , Use here provide,inject The form of will be better , Because business logic requires , Sometimes there may be sun -> Father's situation , And then in submit In the corresponding event , Just sort out the respective sections , The following code :
if (data.researchMethod ) {
data.research_method_code = data.researchMethod.research_method_code;
data.research_method_name = data.researchMethod.research_method_name;
}
This is more troublesome , Although the code is very simple , But when the form needs more information to be verified , You need to write many similar code segments mechanically , Besides , Because the final content of the form may have many formats , For different fields , Also rewrite different key names . More trouble , But when the form verification field is relatively few , It is more convenient to use .
2. utilize computed Method , When getting data , Directly modify the key name .
Personally, I think this method is more clever , It is very clever to deal with the problem of inconsistent attribute names , First of all, will el-select Bind to a value ,Eg: v-model=“transferCenter”. Then listen for this attribute , And make use of getter and setter Operate on it , Go straight to the code :
const transferCenter = computed({
get() {
return {
cost_dept_code: form.cost_dept_code,
cost_dept_name: form.cost_dept_name,
cost_center_code: form.cost_center_code,
cost_center_name: form.cost_center_name,
};
},
set(val) {
form.cost_dept_code = val.cost_dept_code;
form.cost_dept_name = val.cost_dept_name;
form.cost_center_code = val.cost_center_code;
form.cost_center_name = val.cost_center_name;
},
});
Here's an additional explanation computed How to use it ,get(){} function : Execute when the current attribute needs to be read , Calculate and return the value of the current property according to the relevant data ,set(val) {} function : Monitor the change of current attribute value , Execute when the attribute value changes , Update relevant attribute data . Here we directly realize the two-way binding of data , And imperceptibly changed the name of the attribute , Ensured form The attribute field of corresponds to the correct attribute value .
边栏推荐
- Global and Chinese markets for single beam side scan sonar 2022-2028: Research Report on technology, participants, trends, market size and share
- [width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
- How to set an alias inside a bash shell script so that is it visible from the outside?
- 【coppeliasim】6自由度路径规划
- RDD conversion operator of spark
- selenium 等待方式
- How does redis implement multiple zones?
- [flask] official tutorial -part1: project layout, application settings, definition and database access
- Redis-字符串类型
- How to improve the level of pinduoduo store? Dianyingtong came to tell you
猜你喜欢
![[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)](/img/ec/7fcdcbd9c92924e765d420f7c71836.jpg)
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Folio. Ink is a free, fast and easy-to-use image sharing tool

A basic lintcode MySQL database problem

0211 embedded C language learning

Minecraft 1.18.1、1.18.2模组开发 22.狙击枪(Sniper Rifle)

Redis-列表

Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization

Adapter-a technology of adaptive pre training continuous learning

Redis-字符串类型
随机推荐
Xshell 7 Student Edition
MySQL learning notes - subquery exercise
VIM usage guide
How to upgrade kubernetes in place
Global and Chinese markets of screw rotor pumps 2022-2028: Research Report on technology, participants, trends, market size and share
Audio and video engineer YUV and RGB detailed explanation
Leetcode3, implémenter strstr ()
【coppeliasim】高效传送带
Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
leetcode-2. Palindrome judgment
[depth first search] Ji Suan Ke: Betsy's trip
Using SA token to solve websocket handshake authentication
SPI communication protocol
Publish your own toolkit notes using NPM
Gbase 8C database upgrade error
Use the list component to realize the drop-down list and address list
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
[solution] every time idea starts, it will build project
selenium 等待方式
Redis-列表