deyanivanov966

Section 3 Interpolation and Data

Feb 12th, 2022
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. --> Explain Interpolation
  2. What is Interpolation?
  3.  
  4. Interpolation, aka “String Interpolation”, we talk about the “Mustache” syntax{{ }}.
  5. There are 4 types of interpolations:
  6.  
  7. 1. Text
  8. 2. JavaScript Expressions
  9. 3. Raw HTML
  10. 4. Attributes
  11.  
  12. Text
  13.  
  14. 1. <div id="app">
  15. 2. {{ title }}
  16. 3. </div>
  17. 4.
  18. 5. data(){
  19. 6. return {
  20. 7. title: "I'm Interpolating!"
  21. 8. }
  22. 9. }
  23.  
  24. We see that Vue is accessing the data property inside our Vie instance, is grabbing the title and ‘binding’ it’s value inside our {{ title }}.
  25.  
  26. JavaScript Expressions
  27.  
  28. 1. <h1>Home Component 10 + 10 = { { 10+10 } }</h1>
  29.  
  30. Raw HTML
  31. Until here we saw that whatever was inside our Mustache {{ }} was text, or interpreted as text, but what about if we want to bind some HTML? Imagine that we have a JSON where we have stored the content of the website, or we fetch it from the back-end. Most of the time, we will store the content as plain text, but sometimes we might get HTML instead.
  32. For this we will need to use the v-html directive.
  33.  
  34. 1. 1.
  35. 2. <div id="app">
  36. 3. {{ title }}
  37. 4. </div>
  38. 5. The result of first attempt will be <h1 style='color: blue'>I\'m Interpolating</h1>
  39. 6. 2.
  40. 7. <div id="app">
  41. 8. <span v-html="title"></span>
  42. 9. </div>
  43. 10. The result will be "I'm Interpolating" with blue color
  44. 11. data() {
  45. 12. return {
  46. 13. title: "<h1 style='color: blue'>I'm Interpolating</h1>"
  47. 14. }
  48. 15. }
  49.  
  50. Attributes
  51. As we saw with the v-html, there are times when we need to pass certain values to our HTML, for that, we use directives. Imagine we have to put a dynamic id, inside of a div in our HTML. For this we can use the v-bind directive.
  52.  
  53. The v-bind directive is a Vue. js directive used to bind one or more attributes, or a component properties( props ) to an element. If that attribute is bonded to our data defined in Vue instance then dynamically changes can be observed as data changes.
  54.  
  55. 1. <div>
  56. 2. <span v-bind:id="dynamicId"></span>
  57. 3. </div>
  58.  
  59. Directives are special HTML attributes that allow us to manipulate the DOM. Directives are very powerful and we use them every day. Common directives are v-if , v-for and v-model .
  60.  
  61.  
  62. --> Interpolation and Data
  63. Passing date from script to template
  64.  
  65. To pass data from <script> tag to <template> can do it as follow:
  66.  
  67. 1. <template>
  68. 2. <h1>Name: {{ name }}</h1>
  69. 3. </template>
  70. 4. <script>
  71. 5. export default {
  72. 6. name: "Hello",
  73. 7. data() {
  74. 8. return {
  75. 9. name: "Ivan"
  76. 10. }
  77. 11. }
  78. 12. }
  79. 13. </script>
  80.  
  81.  
  82. --> How to define functions and methods
  83. How to define a function or methods? It’s easy, as it was with passing data.
  84.  
  85. 1. <template>
  86. 2. <h1>Name: {{ name }} Age:{{age()}}</h1>
  87. 3. </template>
  88. 4. <script>
  89. 5. export default {
  90. 6. name: "Hello",
  91. 7. data() {
  92. 8. return {
  93. 9. name: "Ivan",
  94. 10. getAge() {
  95. 11. return 25;
  96. 12. }
  97. 13. }
  98. 14. }
  99. 15. }
  100. 16. </script>
  101.  
  102. A Vue method is an object associated with the Vue instance. Functions are defined inside the methods object. Methods are useful when you need to perform some action with v-on directive on an element to handle events. Functions defined inside the methods object can be further called for performing actions.
  103.  
  104. Define a method
  105.  
  106. 1. <template>
  107. 2. <h1>Fist name: {{ name }}</h1>
  108. 3. <h1>Last name: {{ getLastName() }}</h1>
  109. 4. </template>
  110. 5. <script>
  111. 6. export default {
  112. 7. name: "Hello",
  113. 8. data() {
  114. 9. return {
  115. 10. name: "Ivan"
  116. 11. }
  117. 12. },
  118. 13. methods: {
  119. 14. getLastName() {
  120. 15. return "Ivanov";
  121. 16. }
  122. 17. }
  123. 18. }
  124. 19. </script>
  125.  
Advertisement
Add Comment
Please, Sign In to add comment