局部组件的使用

1,如果仅仅是实例化vue对象中,既有el,又有template,如果template中定义模板内容,那么template模板的优先级大于el

2,局部组件的使用

(1),声子:声明的时候要把声明的组件放在上面,然后再在下面的vue中挂子和用子,否则因为挂子中用到子组件的组件名,写在下面的话,挂子的时候找不到组件,所以没有效果,声明组件,Vue中,组件的名字,首字母要大写,跟标签区分开,组建中的data必须是个函数,一定有返回值

 1 let App = {
 2         data(){
 3             return {
 4                text:"我是日天"
 5             }
 6 
 7         },
 8         template:`
 9             <div id="a">
10                 <h2>{{ text }}</h2>
11             </div>
12         `,
13         methods:{
14 
15         }
16     }
View Code

(2),挂子:挂载子组件(在vue中挂载)

1 components:{
2             //2.挂子
3             //如果key和value一样 可以只写一个
4             //App:App
5             App
6         }
View Code

(3), 用子: 使用子组件,template里面必须是闭合标签

1 template: `
2                 <div class="app">
3 
4                  <App></App>
5                 </div>
6 
7             `,
View Code

全局组件的使用

第一个参数是组件的名字,第二个参数options参数,他是全局组件

 1 Vue.component('VBtn',{
 2         data(){
 3             return {
 4 
 5             }
 6         },
 7         template:`<button>
 8             <slot></slot>
 9 
10 </button>`
11     })
View Code

父子组件之间的传值问题

如何区分是父组件还是子组件:

组件通过pros属性接受它的父级的数据,那么当前这个组件就是相对的子组件了,提供数据的就是父组件了。

1,父==>子传值

1,在子组件中,声明props,只要声明了该属性,name就可以在组建中任意使用

2,在父组件通过绑定自定义属性,挂载数据属性

<Son :msg = 'msg'>

<Son :msg = 'msg'>

 1  let Vheader = {
 2         data() {
 3             return {}
 4         },
 5         //只要声明了父组件的属性,就可以使用
 6         props: ['msg', 'post'],
 7         template: `
 8             <div class="child">
 9                 <h2>日天</h2>
10                 <h2>{{ msg }}</h2>
11                 <h3>{{ post.title}}</h3>
12             </div>
13         `
14     }
15     let App = {
16         data() {
17             return {
18                 text: "我是父组件的数据",
19                 post: {
20                     id: 1,
21                     title: 'My Journey with Vue'
22                 }
23             }
24 
25         },
26         template: `
27             <div id="a">
28                 //绑定自定义属性
29                 <Vheader :msg = 'text' v-bind:post = 'post'></Vheader>
30             </div>
31         `,
32         methods: {},
33         components: {
34             Vheader
35         }
36     }
View Code

2,子==>父传值

1,在子组件中,通过 this.$emit('方法名',值)

2,在父组件中,绑定自定义事件

 1 let Vheader = {
 2         data() {
 3             return {}
 4         },
 5         //只要声明了父组件的属性,就可以使用
 6         props: ['msg', 'post'],
 7         template: `
 8             <div class="child">
 9                 <h1>我是header组件</h1>
10                 <h2>日天</h2>
11                 <h2>{{ msg }}</h2>
12                 <h3>{{ post.title}}</h3>
13                 <VBtn v-bind:id = 'post.id' @clickHandler="clickHandler"></VBtn>
14             </div>
15         `,
16         methods:{
17           clickHandler(val){
18               alert(val);
19               //('父组件声明自定义的事件','值')
20               this.$emit('fatherHandler',val)
21           }
22         },
23          created(){
24           console.log(this);
25         },
26     }
27     let App = {
28         data() {
29             return {
30                 text: "我是父组件的数据",
31                 post: {
32                     id: 1,
33                     title: 'My Journey with Vue'
34                 }
35             }
36         },
37         template: `
38             <div id="a">
39                 我是父组件的 {{post.id}}
40                 <Vheader :msg = 'text' v-bind:post = 'post' @fatherHandler = 'father_handler'></Vheader>
41             </div>
42         `,
43         methods: {
44             //绑定自定义事件
45             father_handler(val){
46                 console.log(val);
47 
48                 this.post.id = val;
49             }
50         },
51         components: {
52             Vheader
53         },
54          created(){
55           console.log(this);
56         },
57     }
View Code

3,平行组件传值

使用bus公交车对象:绑定$son和$emit

let bus = new Vue();

A==>B传值 : $emit('函数名',值)===>$on('函数名',()=>{ })

 1 let bus = new Vue();
 2     //A===》B   B要声明事件  $on('事件的名字',function(val){})  A要触发事件 $emit('A组件中声明的事件名','值')
 3 
 4     //前提 这两个方法必须绑定在同一个实例化对象(bus)
 5     Vue.component('Test2', {
 6         data() {
 7             return {
 8                 text:''
 9             }
10         },
11         template: `
12             <h2>{{ text }}</h2>
13         `,
14         methods: {
15 
16         },
17         created(){
18             bus.$on('testData', (val)=> {
19                 alert(val);
20                 this.text = val;
21             })
22         }
23     })
24     Vue.component('Test', {
25         data() {
26             return {
27                 msg: '我是子组件的数据'
28             }
29         },
30         props:['txt'],
31         template: `
32             <button @click = 'clickHandler'>{{ txt }}</button>
33         `,
34         methods: {
35             clickHandler() {
36 
37                 bus.$emit('testData',this.msg)
38             }
39         }
40     })
View Code