1. Props communication
Note: The camel naming props of the DOM template should be converted to short horizontal segmentation naming.
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div > <my-component message='Data from parent component' warning-text="Prompt message"></my-component> </div> <script src="/vue/2.5.9/"></script> <script type="text/javascript"> ('my-component', { template: '<div>{{warningText}}:{{message}}</div>', props: ['message', 'warningText'] }) new Vue({ el: "#app" }) </script> </body> </html>
Pass dynamic data (v-bind):
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div > <input type="text" v-model="parentMessage" /> <my-component :message='parentMessage'></my-component> </div> <script src="/vue/2.5.9/"></script> <script type="text/javascript"> ('my-component', { template: '<div>{{message}}</div>', props: ['message'] }) new Vue({ el: "#app", data: { parentMessage: '' }//Front-end full stack development exchange group: })//866109638 </script>//Help 1-3 years of staff </body>//Break through technical bottlenecks and improve thinking ability </html>
2. One-way data flow
(1) The child component saves the values passed by the parent component and modify and use them under the child component's own scope.
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head> <body> <div > <my-component :init-count='1'></my-component> </div> <script src="/vue/2.5.9/"></script> <script type="text/javascript"> ('my-component', { template: '<div>{{initCount}}</div>', props: ['initCount'], data: function() { return } }) new Vue({ el: "#app" }) </script> </body> </html>
(2) Use calculation properties
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>Vue</title> </head>//Front-end full-stack communication and learning circle://866109386, help front-end personnel for 1-3 years <body>//Break through technical bottlenecks and improve thinking ability <div > <my-component :width='100'></my-component> </div> <script src="/vue/2.5.9/"></script> <script type="text/javascript"> ('my-component', { template: '<div :style="style">Component content</div>', props: ['width'], computed: { style: function() { return { width: + 'px' } } } }) new Vue({ el: "#app" }) </script> </body> </html>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.