current position:Home>[Vue] deeper understanding of user-defined attribute props
[Vue] deeper understanding of user-defined attribute props
2022-05-15 02:10:10【Triumph-light】
Due to the recent completion of study vue Working on projects , But when working on projects with , One problem I found was props The custom attribute can be read or written when it appears , And the data flow has become a two-way problem .
initial props
When I first learned , about props My understanding is Can only read but not write , At the same time, it is also used as a tool for value transfer between components , Parent component to child component One way data flow
The emergence of problems
Put code here :
On the page vue To check the data, the data has indeed changed
But when working with others on projects , It is found that there is a two-way flow of data in the parent and child components , also eslint The specification reported an error , Check today props When documenting , The text is as follows :
be-all prop Both make their father and son prop A one-way down binding is formed between : Parent prop The update flows down to the child component , But not the other way around . This prevents unexpected changes to the state of the parent component from the child component , This causes your application's data flow to be difficult to understand .
Find out props The one-way flow of data is not fixed , It usually flows in one direction , But when the data is objects and arrays , Yes, two-way flow can occur
Why do two-way flows occur ?
reason : Pay attention to JavaScript Objects and arrays are passed in by reference , So for an array or object type prop Come on , Changing the object or array itself in a child component will affect the state of the parent component .
summary
1、 In order to standardize your code writing , In the future, I will promise to take this prop Used to pass an initial value ; This subcomponent next wants to use it as a local prop When using data , Store it in data In the data ,data The data is readable and writable
2、 For the problem of two-way flow , Don't modify it directly props Value , When the parent component needs the value of the child component , Then pass the value from the child to the parent ( There is another problem here , Subsequently added )
Add questions
It was said that props Custom attributes cannot be written , In fact, it is to limit the flow of data , So read only, not write ,
So here arrays and objects can be written .
Code proof :
this.tableData = this.tableData.filter((item, index) => index !== i)
const id = this.tableData[i].id
But in the end, in order to standardize , Or saved the data to data, Code changes have been made . And there is also the problem of data asynchrony ( It has been solved in the following article ).
If there's something wrong with what I recorded , I hope you can correct me !!!
copyright notice
author[Triumph-light],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205120526195830.html
The sidebar is recommended
- Educoder web programming foundation autumn 2021 - HTML5 - text control class label
- Educoder web programming foundation autumn 2021 - HTML5 document header tags and attributes
- Educoder web programming foundation autumn 2021 - HTML5 - text control class label
- Educoder web programming foundation autumn 2021 - overview of web front end development
- Vue data broker
- HTML5 notes - new specifications and new elements
- Lunch break train & problem thinking: on multidimensional array statistics of the number of elements
- Children's Painting Tutorial | children's interesting art lesson "firecrackers say goodbye to the old year and fireworks greet the new year"
- LeetCode 713. Subarray with product less than k
- JavaScript programming training Day6
guess what you like
Vue realizes two-level linkage
Tengine uses brotli to turn on tls1 3 and optimize HTTPS access speed
C + +: remove the specified element from the array
C + +: main elements
Learn about nginx and tomact
IC workplace Theory -- feelings of senior brother of digital ic front-end design after 4 months of employment (Part I)
Vue Chapter 3 scaffolding's most complete render function, props, mixin mixing, plug-ins, browser local storage, component customization events_ Binding, unbinding, global event bus, message subscription and publishing, nexttick, Vue transition and animat
Vue Chapter 5 vuex creates and introduces the use of store, getters and four map methods
***************2、 JS + Vue js***************
A detailed explanation of HTTP protocol + classic interview questions
Random recommended
- Vue Chapter 4 Ajax in Vue: cross domain problems and slot slots
- Online nginx does not support the bug record of error reporting in delete request
- Okhttp sets the public parameters and puts the JSON object into the @ body to request the configuration of the public parameters
- Fluent ignores authentication of HTTPS and WSS certificates
- Browser loading HTML page evokes the most complete strategy of APP in mobile phone
- HTTP status code 304 and Etag details
- Using angularjs for fuzzy dynamic search drop-down box content
- Seamless connection to Macao, Hengqin Island, which is beautiful beyond the sky, is on fire again
- JQuery framework
- Vue mouse down and move event
- A simple Vue answer demo, single choice and multiple choice
- Front end interview question: if the cell in the table is red, click it to turn into white; if it is white, click it to turn into red, and what is not clicked is white
- Vue packaging folding panel plus animation effect
- If the front-end does real-time data acquisition?
- Ant vue3 table sorted by time
- Vue local refresh iframe
- Vue instruction authority
- Vue iframe refresh remains on the original page
- Vue realizes Gaode map API mask, positioning and weather
- Comparison and implementation of bidirectional data binding between react and Vue
- React error report record
- Analysis of react life cycle function
- react demo1 TodoList
- Summary of the differences between state and props in react
- Difference between controlled components and uncontrolled components in react
- Node. JS use get Stream download file
- How does JavaScript copy arrays instead of references
- JavaScript calculates the number of days, hours, minutes and seconds between days
- JQuery get page address parameters
- JavaScript create Download
- node. JS traversal directory
- Tips for using JavaScript string split (how to keep the delimiter)
- Common CSS notes
- JavaScript usage Get the address of the regular function
- New generation node JS web development framework koa zero foundation entry learning notes
- JavaScript notes
- asp. Net core method for modifying HTML without rerun
- Summary of compatibility processing of H5 + Vue cli project in Safari browser
- Summary of amap scheme of Gaode map used in vue3 + TS project
- Summary of filter scheme used in JS code in Vue