Advertisement
deyanivanov966

Section 2 Vue components

Feb 12th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. --> Vue components
  2. What are Vue Components?
  3.  
  4. One important feature of Vue is the ability to use components. Components are reusable Vue instances with custom HTML elements. Components can be reused as many times as you want or used in another component, making it a child component. Data, computed, watch, and methods can be used in a Vue component.
  5.  
  6.  
  7. --> Vue components
  8. Create Vue Component
  9.  
  10. 1.Inside src/components directory, create a new file named Home .vue. Open the file in your editor.
  11. 2.Create a component template tab by adding <template> </template> at the top of the file.
  12. 1. <template> Your template can contain any valid HTML tag, as well as some specific Vue syntax.
  13. 3.Create a <script> </script> tab below the template tab. Inside the <script> tags, add the exported default {}, which is your component.
  14. 1. The <script> section contains all the logic of your component. Most importantly, your <script> tag must have at least one exported JS object by default. This object is the place where you can locate components locally, define component inputs (props), process a state locally, define methods.
  15.  
  16.  
  17. --> Vue components
  18. Template and Script
  19.  
  20. Define tag <template> </template> and add the following tag <h2>This is my Home page </h2>.
  21. Next, lest add name for our component inside of <script></script>:
  22.  
  23. 1. exported default {
  24. 2. name: 'Home'
  25. 3. }
  26.  
  27.  
  28. --> Vue components
  29. Import and Use Component
  30.  
  31. To be able to use this component we need to import it.
  32. Open App.vue file and add this line of code inside of <script></script> tag:
  33. import Home from ‘./components/Home.vue’;
  34. Inside export default {} we need to add component name.
  35.  
  36. 1. export default {
  37. 2. name: 'App',
  38. 3. components: {
  39. 4. HelloWorld,
  40. 5. Home
  41. 6. }
  42. 7. }
  43.  
  44.  
  45. --> Vue components
  46. Add Style
  47. To add style (css) to our component we need to use the tag <style></style>.
  48.  
  49. Lets change the color of our h2 tag that we add previously.
  50.  
  51. Inside of our tag <style></style> add the following lines:
  52.  
  53. 1. <style>
  54. 2. h2 {
  55. 3. color: yellow;
  56. 4. }
  57. 5. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement