黑马vue---56-58、vue组件创建的三种方式

黑马vue---56-58、vue组件创建的三种方式

一、总结

一句话总结:

不论是哪种方式创建出来的组件,组件的 template 属性指向的模板内容,必须有且只能有唯一的一个根元素

1、使用 Vue.extend 来创建全局的Vue组件?

Vue.component('mycom1', Vue.extend({
Vue.component('mycom1', Vue.extend({
    template: '<h3>这是使用 Vue.extend 创建的组件</h3>'
}))

2、使用vue的component方法直接创建?

Vue.component('mycom2', {
Vue.component('mycom2', {
    template: '<div><h3>这是直接使用 Vue.component 创建出来的组件</h3><span>123</span></div>'
})

3、使用template标签直接创建?

在 被控制的 #app 外面,使用 template 元素,定义组件的HTML模板结构
在 Vue.component中直接指定template: '#tmpl'即可
  <!-- 在 被控制的 #app 外面,使用 template 元素,定义组件的HTML模板结构  -->
  <template id="tmpl">
    <div>
      <h1>这是通过 template 元素,在外部定义的组件结构,这个方式,有代码的只能提示和高亮</h1>
      <h4>好用,不错!</h4>
    </div>
  </template>

    Vue.component('mycom3', {
      template: '#tmpl'
    })

4、vue引入组件的方式?

使用标签的形式,直接引入即可:比如<mycom2></mycom2>
Vue.component('mycom2', {
    template: '<div><h3>这是直接使用 Vue.component 创建出来的组件</h3><span>123</span></div>'
})

5、vue组件创建组件最便捷方式?

使用template标签直接在vue控制的区域之外创建,然后在 Vue.component中直接指定template: '#tmpl'即可
  <!-- 在 被控制的 #app 外面,使用 template 元素,定义组件的HTML模板结构  -->
  <template id="tmpl">
    <div>
      <h1>这是通过 template 元素,在外部定义的组件结构,这个方式,有代码的只能提示和高亮</h1>
      <h4>好用,不错!</h4>
    </div>
  </template>

    Vue.component('mycom3', {
      template: '#tmpl'
    })

二、vue组件创建的三种方式

1、使用 Vue.extend 来创建全局的Vue组件

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5   <meta charset="UTF-8">
 6   <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7   <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8   <title>Document</title>
 9   <script src="./lib/vue-2.4.0.js"></script>
10 </head>
11 
12 <body>
13   <div id="app">
14     <!-- 如果要使用组件,直接,把组件的名称,以 HTML 标签的形式,引入到页面中,即可 -->
15     <mycom1></mycom1>
16   </div>
17 
18   <script>
19     // 1.1 使用 Vue.extend 来创建全局的Vue组件
20     // var com1 = Vue.extend({
21     //   template: '<h3>这是使用 Vue.extend 创建的组件</h3>' // 通过 template 属性,指定了组件要展示的HTML结构
22     // })
23     // 1.2 使用 Vue.component('组件的名称', 创建出来的组件模板对象)
24     // Vue.component('myCom1', com1)
25     // 如果使用 Vue.component 定义全局组件的时候,组件名称使用了 驼峰命名,则在引用组件的时候,需要把 大写的驼峰改为小写的字母,同时,两个单词之前,使用 - 链接;
26     // 如果不使用驼峰,则直接拿名称来使用即可;
27     // Vue.component('mycom1', com1)
28 
29     // Vue.component 第一个参数:组件的名称,将来在引用组件的时候,就是一个 标签形式 来引入 它的
30     // 第二个参数: Vue.extend 创建的组件  ,其中 template 就是组件将来要展示的HTML内容
31     Vue.component('mycom1', Vue.extend({
32       template: '<h3>这是使用 Vue.extend 创建的组件</h3>'
33     }))
34 
35 
36     // 创建 Vue 实例,得到 ViewModel
37     var vm = new Vue({
38       el: '#app',
39       data: {},
40       methods: {}
41     });
42   </script>
43 </body>
44 
45 </html>
 

2、使用vue的component方法直接创建

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5   <meta charset="UTF-8">
 6   <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7   <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8   <title>Document</title>
 9   <script src="./lib/vue-2.4.0.js"></script>
10 </head>
11 
12 <body>
13   <div id="app">
14     <!-- 还是使用 标签形式,引入自己的组件 -->
15     <mycom2></mycom2>
16   </div>
17 
18   <script>
19     // 注意:不论是哪种方式创建出来的组件,组件的 template 属性指向的模板内容,必须有且只能有唯一的一个根元素
20     Vue.component('mycom2', {
21       template: '<div><h3>这是直接使用 Vue.component 创建出来的组件</h3><span>123</span></div>'
22     })
23 
24     // 创建 Vue 实例,得到 ViewModel
25     var vm = new Vue({
26       el: '#app',
27       data: {},
28       methods: {}
29     });
30   </script>
31 </body>
32 
33 </html>

3、使用template标签直接创建

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5   <meta charset="UTF-8">
 6   <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7   <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8   <title>Document</title>
 9   <script src="./lib/vue-2.4.0.js"></script>
10 </head>
11 
12 <body>
13   <div id="app">
14     <mycom3></mycom3>
15     <!-- <login></login> -->
16   </div>
17 
18 
19   <div id="app2">
20     <mycom3></mycom3>
21     <login></login>
22   </div>
23 
24   <!-- 在 被控制的 #app 外面,使用 template 元素,定义组件的HTML模板结构  -->
25   <template id="tmpl">
26     <div>
27       <h1>这是通过 template 元素,在外部定义的组件结构,这个方式,有代码的只能提示和高亮</h1>
28       <h4>好用,不错!</h4>
29     </div>
30   </template>
31 
32   <template id="tmpl2">
33     <h1>这是私有的 login 组件</h1>
34   </template>
35 
36   <script>
37     Vue.component('mycom3', {
38       template: '#tmpl'
39     })
40 
41     // 创建 Vue 实例,得到 ViewModel
42     var vm = new Vue({
43       el: '#app',
44       data: {},
45       methods: {}
46     });
47 
48 
49     var vm2 = new Vue({
50       el: '#app2',
51       data: {},
52       methods: {},
53       filters: {},
54       directives: {},
55       components: { // 定义实例内部私有组件的
56         login: {
57           template: '#tmpl2'
58         }
59       },
60 
61       beforeCreate() { },
62       created() { },
63       beforeMount() { },
64       mounted() { },
65       beforeUpdate() { },
66       updated() { },
67       beforeDestroy() { },
68       destroyed() { }
69     })
70   </script>
71 </body>
72 
73 </html>
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/11771111.html