Vue.js组件的使用

runner-up 2021-05-03 18:56:05
vue JS 组件 Vue.js 使用


Vue.js组件的使用

一.组件

组件是可复用的vue实例,可分为局部组件和全局组件。

二.组件入门小案例

要求定义一个组件”one“,并重复使用它。

2.1.代码实例

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue局部组件和全局组件</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 //创建vue实例对象
18 var vm = new Vue({
19 //挂载点
20  el:"#app",
21 //局部组件需要注册
22  components:{
23 //注册局部组件
24  one:{
25 //局部组件模板
26  template:"<ul><li>吃饭</li><li>睡觉</li><li>打豆豆</li></ul>"
27  }
28  }
29  })
30 </script>

2.2.执行效果

 

三.将组件的模板写在<body>标签内

3.1.实例代码

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue局部组件和全局组件</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>吃饭2</li>
17 <li>睡觉2</li>
18 <li>打豆豆2</li>
19 </ul>
20 </p>
21 </div>
22 </template>
23 </body>
24 </html>
25 <script>
26 //创建vue实例对象
27 var vm = new Vue({
28 //挂载点
29  el:"#app",
30 //局部组件需要注册
31  components:{
32 //注册局部组件
33  one:{
34 //局部组件模板
35  template:"<ul><li>吃饭</li><li>睡觉</li><li>打豆豆</li></ul>"
36  },
37  comtwo:{
38  template:"#two"
39  }
40  }
41  })
42 </script>

3.2.效果图

 

四.局部组件数据的使用

各个局部组件的数据是相互独立的;实现点击某个组件,相应的该组件内容数字+1的功能

4.1.实例代码

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue局部组件和全局组件</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>吃饭2</li>
33 <li>睡觉2</li>
34 <li>打豆豆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 //创建vue实例对象
46 var vm = new Vue({
47 //挂载点
48  el:"#app",
49 //局部组件需要注册
50  components:{
51 //注册局部组件
52  one:{
53 //局部组件模板
54  template:"<ul><li>吃饭</li><li>睡觉</li><li>打豆豆</li></ul>"
55  },
56  two:{
57  template:"#two"
58  },
59 //局部组件三
60  three:{
61  template:"#three",
62 //组件数据
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.效果图

 

 5.全局组件

可以在任何地方使用的组件;全局组件需要通过vue的属性component去创建;

5.1.实例代码

 1 <html lang="en">
 2 <head>
 3 <meta charset="UTF-8">
 4 <title>vue局部组件和全局组件</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>吃饭2</li>
34 <li>睡觉2</li>
35 <li>打豆豆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>么么哒~~~</p>
46 </template>
47 </body>
48 </html>
49 <script>
50 //vue的全局组件
51 //全局组件可以在任意地方使用,需要使用vue实例的component属性创建
52  Vue.component("four",{
53  template:"#four"
54  });
55 //创建vue实例对象
56 var vm = new Vue({
57 //挂载点
58  el:"#app",
59 //局部组件需要注册
60  components:{
61 //注册局部组件
62  one:{
63 //局部组件模板
64  template:"<ul><li>吃饭</li><li>睡觉</li><li>打豆豆</li></ul>"
65  },
66  two:{
67  template:"#two"
68  },
69 //局部组件三
70  three:{
71  template:"#three",
72 //组件数据
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.效果图

 

版权声明
本文为[runner-up]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/runner-up/p/14727919.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