The use of vue.js component

runner-up 2021-05-03 18:56:32
use vue.js vue js component


Vue.js Use of components

One . Components

Components are reusable vue example , It can be divided into local component and global component .

Two . Component introduction case

It is required to define a component ”one“, And reuse it .

2.1. Code instance

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue Local components and global components </title>
 5 <script src="./js/vue.min.js"></script>
 6 </head>
 7 <body>
 8 <div id="app">
 9 <one></one>
10 <one></one>
11 <one></one>
12 <one></one>
13 </div>
14 </body>
15 </html>
16 <script>
17 // establish vue Instance object 
18 var vm = new Vue({
19 // Mount point 
20  el:"#app",
21 // Local components need to be registered 
22  components:{
23 // Register local components 
24  one:{
25 // Local component template 
26  template:"<ul><li> having dinner </li><li> sleep </li><li> Doudou </li></ul>"
27  }
28  }
29  })
30 </script>

2.2. Execution effect

 

3、 ... and . Write the template of the component in <body> tag

3.1. The sample code

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue Local components and global components </title>
 5 <script src="./js/vue.min.js"></script>
 6 </head>
 7 <body>
 8 <div id="app">
 9 <one></one>
10 <comtwo></comtwo>
11 </div>
12 <template id="two">
13 <div>
14 <p>
15 <ul>
16 <li> having dinner 2</li>
17 <li> sleep 2</li>
18 <li> Doudou 2</li>
19 </ul>
20 </p>
21 </div>
22 </template>
23 </body>
24 </html>
25 <script>
26 // establish vue Instance object 
27 var vm = new Vue({
28 // Mount point 
29  el:"#app",
30 // Local components need to be registered 
31  components:{
32 // Register local components 
33  one:{
34 // Local component template 
35  template:"<ul><li> having dinner </li><li> sleep </li><li> Doudou </li></ul>"
36  },
37  comtwo:{
38  template:"#two"
39  }
40  }
41  })
42 </script>

3.2. design sketch

 

Four . Use of local component data

The data of each local component is independent of each other ; To click on a component , The corresponding component content number +1 The function of

4.1. The sample code

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue Local components and global components </title>
 5 <script src="./js/vue.min.js"></script>
 6 <style>
 7  *{
 8  margin: 0px;
 9  padding: 0px;
10 }
11  .myp{
12  width: 100px;
13  height: 100px;
14  background-color: greenyellow;
15  float:left;
16  margin-left:12px;
17 }
18 </style>
19 </head>
20 <body>
21 <div id="app">
22 <one></one>
23 <two></two>
24 <three></three>
25 <three></three>
26 <three></three>
27 </div>
28 <template id="two">
29 <div>
30 <p>
31 <ul>
32 <li> having dinner 2</li>
33 <li> sleep 2</li>
34 <li> Doudou 2</li>
35 </ul>
36 </p>
37 </div>
38 </template>
39 <template id="three">
40 <p class="myp" @click="add">{{msg}}</p>
41 </template>
42 </body>
43 </html>
44 <script>
45 // establish vue Instance object 
46 var vm = new Vue({
47 // Mount point 
48  el:"#app",
49 // Local components need to be registered 
50  components:{
51 // Register local components 
52  one:{
53 // Local component template 
54  template:"<ul><li> having dinner </li><li> sleep </li><li> Doudou </li></ul>"
55  },
56  two:{
57  template:"#two"
58  },
59 // Local component 3 
60  three:{
61  template:"#three",
62 // Component data 
63  data(){
64 return{
65  msg:1
66  }
67  },
68  methods:{
69  add(){
70 this.msg++;
71  }
72  }
73  }
74  }
75  })
76 </script>

4.2. design sketch

 

 5. Global components

Components that can be used anywhere ; The global component needs to go through vue Properties of component To create ;

5.1. The sample code

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue Local components and global components </title>
 5 <script src="./js/vue.min.js"></script>
 6 <style>
 7  *{
 8  margin: 0px;
 9  padding: 0px;
10 }
11  .myp{
12  width: 100px;
13  height: 100px;
14  background-color: greenyellow;
15  float:left;
16  margin-left:12px;
17 }
18 </style>
19 </head>
20 <body>
21 <div id="app">
22 <one></one>
23 <two></two>
24 <three></three>
25 <three></three>
26 <three></three>
27 <four></four>
28 </div>
29 <template id="two">
30 <div>
31 <p>
32 <ul>
33 <li> having dinner 2</li>
34 <li> sleep 2</li>
35 <li> Doudou 2</li>
36 </ul>
37 <four></four>
38 </p>
39 </div>
40 </template>
41 <template id="three">
42 <p class="myp" @click="add">{{msg}}</p>
43 </template>
44 <template id="four">
45 <p> mua ~~~</p>
46 </template>
47 </body>
48 </html>
49 <script>
50 //vue The global component of 
51 // Global components can be used anywhere , Need to use vue Example of component Attributes to create 
52  Vue.component("four",{
53  template:"#four"
54  });
55 // establish vue Instance object 
56 var vm = new Vue({
57 // Mount point 
58  el:"#app",
59 // Local components need to be registered 
60  components:{
61 // Register local components 
62  one:{
63 // Local component template 
64  template:"<ul><li> having dinner </li><li> sleep </li><li> Doudou </li></ul>"
65  },
66  two:{
67  template:"#two"
68  },
69 // Local component 3 
70  three:{
71  template:"#three",
72 // Component data 
73  data(){
74 return{
75  msg:1
76  }
77  },
78  methods:{
79  add(){
80 this.msg++;
81  }
82  }
83  }
84  }
85  })
86 </script>

5.2. design sketch

 

版权声明
本文为[runner-up]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210503185450742D.html

  1. CSS layout
  2. Application scenario explanation of Vue dynamic component
  3. Redux learning notes 04 -- using multiple reducers to manage data
  4. After three months of typescript writing, what have I learned?
  5. Node family - what is a callback?
  6. React -- a simple implementation of render & create element
  7. JS learning simple usage of jquery
  8. Seamless love
  9. 小白前端入门笔记(12),设置哑链接
  10. Small white front-end entry notes (12), set dumb links
  11. Vue2. X opens composition API and TSX
  12. Interview record and thinking of social recruitment for one and a half years (Alibaba, Tencent, baidu offer)
  13. Flex learning notes
  14. The most essential closure article in the eastern hemisphere
  15. 2021-05-03 hot news
  16. Sword finger offer -- reverse order pair in array (JS Implementation)
  17. Working process of scaffold
  18. Use decorator mode to strengthen your fetch
  19. [JS] scope (Introduction)
  20. Employment information statistics network (interface document)
  21. Analysis of MVC
  22. [middle stage] please stay and join me in the backstage
  23. Understanding front end garbage collection
  24. [continuous update] front end special style implementation
  25. Flutter product analysis and package reduction scheme
  26. XPath positioning
  27. 前端开发css中的flex布局的使用
  28. The use of flex layout in front end development CSS
  29. JQuery核心函数和静态方法
  30. JQuery core functions and static methods
  31. Node family - understanding of blocking and non blocking
  32. 热点微前端Microfrontend的讨论:谷歌AdWords是真实的微前端
  33. Vue source code analysis (2) initproxy initialization proxy
  34. What's TM called react diff
  35. Summary of common front end data structure
  36. Useeffect in hooks
  37. [encapsulation 02 design pattern] Command pattern, share meta pattern, combination pattern, proxy pattern, strategy pattern
  38. Front end notes: virtual Dom and diff of vue2. X
  39. The best code scanning plug-in of flutter
  40. The simplest plug-in for rights management of flutter
  41. 21. Object oriented foundation "problems and solutions of object traversal"
  42. Discussion on hot micro front end: Google AdWords is a real micro front end
  43. Usecallback and usememo for real performance optimization
  44. 【前端圭臬】十一:从规范看 JavaScript 执行上下文(下)
  45. [front end standard] 11: Javascript execution context from the perspective of specification (2)
  46. Hexagonal六角形架构ReactJS的实现方式 - Janos Pasztor
  47. Transaction of spring's reactive / imperative relational database
  48. The implementation of hexagonal hexagonal reactjs Janos pasztor
  49. HTTP状态码:402 Payment Required需要付款 - mozilla
  50. HTTP status code: 402 payment required - Mozilla
  51. Factory mode, constructor mode and prototype mode
  52. Build the scaffold of react project from scratch (Series 1: encapsulating a request method with cache function based on Axios)
  53. Cocos Quick Start Guide
  54. Comparison of three default configurations of webpack5 modes
  55. A case study of the combination of flutter WebView and Vue
  56. CSS: BFC and IFC
  57. A common error report and solution in Vue combat
  58. JS: this point
  59. JS: prototype chain
  60. JavaScript series -- promise, generator, async and await