vue基础----组件通信(props,$emit,$attrs,$listeners)

一、父传子,子传孙

  1. props

    1>在父组件中通过子组件自定义的标签属性来传递数据。

    2>在子组件中通过props声明希望用到的数据

 1 <body>
 2     <div id="app">
 3         <my-father :msg1="msg" a="10" :b="20" @click="fn"></my-father>
 4     </div>
 5     <script src="./node_modules/vue/dist/vue.js"></script>
 6     <script>
 7         let vm = new Vue({
 8             el:"#app",
 9             data:{
10                 msg:"hello yilia"
11             },
12             methods:{
13                 fn(){
14                     console.log("father");
15                 }
16             },
17             components:{
18                 "my-father":{
19                     props:[‘msg1‘],
20                     template:`<div><h1>{{msg1}}</h1><my-son :msg2="msg1"></my-son></div>`,
21                     data(){
22                         return {
23                         }
24                     },
25                     components:{
26                         "my-son":{
27                             props:[‘msg2‘],
28                             template:`<p>{{msg2}}</p>`,
29                             inheritAttrs:true, //为false的时候,没有用到的数据不会显示在dom结构上
30                             data(){
31                                 return{
32                                 }
33                             }
34                         }
35                     }
36                 }
37             }
38         });
39
40     </script>
41
42 </body>

1.1这里需要注意的props 除了上述这种写法,还可以写成对象形式,来校验数据

 1 props: {
 2 a: {
 3     type: Number,
 4     default: 10
 5 },
 6 b: {
 7     type: String,
 8     validator(val) {
 9         return val>0; // "2">0
10     }
11 },
12 arr: {
13     type: Array,
14     //假如属性是数组或对象 默认值需要通过函数返回
15     default:()=>([1])
16 }
17 },

2.有时候my-father这块用不到数据,但需要把爷爷的数据传给孙子,可以用$attrs,在 my-son v-bind="$attrs"

this.$attrs 对没有使用的属性保留在this.$attrs (也就是props中没有申明的属性)

 1 <body>
 2     <div id="app">
 3         <my-father :msg1="msg" a="10" :b="20" @click="fn"></my-father>
 4     </div>
 5     <script src="./node_modules/vue/dist/vue.js"></script>
 6     <script>
 7         let vm = new Vue({
 8             el:"#app",
 9             data:{
10                 msg:"hello Yilia"
11             },
12             methods:{
13                 fn(){
14                     console.log("father");
15                 }
16             },
17             components:{
18                 "my-father":{
19                  //   props:[‘msg1‘],
20                     template:`<div><h1></h1><my-son v-bind="$attrs"></my-son></div>`,
21                     data(){
22                         return {
23                         }
24                     },
25                     components:{
26                         "my-son":{
27                             props:[‘msg1‘],
28                             template:`<p>{{msg1}}</p>`,
29                             inheritAttrs:true, //为false的时候,没有用到的数据不会显示在dom结构上
30                             data(){
31                                 return{
32                                 }
33                             }
34                         }
35                     }
36                 }
37             }
38         });
39     </script>
40 </body>

二、点击子组件,调用父组件的方法 (想在父组件中绑定原生事件给组件)

1.需要添加修饰符native,不添加就被当作一个属性对待

 1 <body>
 2     <div id="app">
 3         <!--想在父组件中绑定原生事件给组件 需要加.native 不加就被当作一个属性看待-->
 4         <my-button @click.native="fn"></my-button>
 5     </div>
 6     <script src="./node_modules/vue/dist/vue.js"></script>
 7     <script>
 8         let vm = new Vue({
 9             el: "#app",
10             methods:{
11                 fn() {
12                     console.log("fn() is called");
13                 }
14             },
15             components: {
16                 "MyButton": {
17                     template: `
18                     <div>
19                         点我
20                     </div>`
21                 }
22             }
23         });
24     </script>
25 </body>

点击 “点我” 的时候父组件的fn函数被调用。

2.$listeners 绑定所有的方法,直接调用父组件的方法

 1 <body>
 2     <div id="app">
 3         <!--想在父组件中绑定原生事件给组件 需要加.native 不加就被当作一个属性看待-->
 4         <my-button @click="fn"></my-button>
 5     </div>
 6     <script src="./node_modules/vue/dist/vue.js"></script>
 7     <script>
 8         let vm = new Vue({
 9             el: "#app",
10             methods:{
11                 fn() {
12                     console.log("fn() is called");
13                 }
14             },
15             components: {
16                 "MyButton": {
17                     mounted(){
18                         console.log(this.$listeners);
19                         //{click: ƒ}
20                     },
21                     template: `<div @click="$listeners.click()">点我</div>`
22                 }
23             }
24         });
25     </script>
26 </body>

3.子组件想调用父组件的方法 $emit

1> 在子组件中调用$emit()方法发布一个事件

2> 在父组件中提供一个在子组件内部发布的事件处理函数

3> 在父组件订阅子组件内部发布的事件

 1 <body>
 2     <div id="app">
 3         <!--想在父组件中绑定原生事件给组件 需要加.native 不加就被当作一个属性看待-->
 4         <!--<my-button @click.native="fn"></my-button>-->
 5         <my-button @click="fn" @mouseup="fn"></my-button>
 6     </div>
 7     <script src="../01-vue-basic/code/node_modules/vue/dist/vue.js"></script>
 8     <script>
 9         // 组件通信 props $emit $attrs $listeners
10         /*
11            子如何传父
12            1 在子组件中调用$emit()方法发布一个事件
13            2 在父组件中提供一个在子组件内部发布的事件处理函数
14            3 在父组件订阅子组件内部发布的事件
15         */
16         let vm = new Vue({
17             el: "#app",
18             data: {
19                 content: "点我"
20             },
21             methods:{
22                 fn(num) {
23                     console.log(num,"fn() is called");
24                 }
25             },
26             components: {
27                 "MyButton": {
28                     mounted() {
29                         console.log(this.$listeners);// 绑定所有的方法
30                     },
31                     template: `
32                        <div>
33                          <button @click="$listeners.click(123);">点我</button>
34                          <button @click="$emit(‘click2‘,23)">点我</button>
35                          <button @click="$listeners.click(123);" @mouseup="$listeners.mouseup(123);">点我</button>
36                          <button v-on="$listeners" >点我</button>
37                        </div>
38                     `
39                 }
40             }
41         });
42     </script>
43 </body>

  

原文地址:https://www.cnblogs.com/moon-yyl/p/11613787.html

时间: 2024-07-31 03:17:16

vue基础----组件通信(props,$emit,$attrs,$listeners)的相关文章

vue基础----组件通信($parent,$children)

1.按照dom的父子级关系,在子组件中可以通过$parent 直接调用父组件的方法,也可得到父组件的属性. 2.在父组件中通过$childrens可以得到一个子组件数组,能够在父组件中调用子组件的方法和得到属性. 3.特别注意一下_uid标识每一个组件. 下面是一个下拉菜单举例 1 <body> 2 <div id="app"> 3 <collapse> 4 <collapse-item title="大学同学">大学

vue基础内容{通信,注册,路由,组件}

ES6常用语法 -- 变量 -- var 变量提升 -- let  {} -- const 常量 -- 模板字符串 -- `` -- ${} -- 函数 -- 箭头函数 -- this -- 普通函数取决于函数最近的调用者 -- 箭头函数取决于当前上下文环境 -- 数据的解构 -- 注意语法 -- 类 -- class 定义类 -- extends 继承 -- constructor 构造方法 -- 子类是没有this的 用super() -- export import -- export {

Vue 父子组件传值 props

1.Vue 的渲染周期: vue 如何实现响应式追踪. 父子组件通信有很多方式,今天单独聊下props 的方式.我们通过查找官方文档很容发现,props传值有静态和动态两种传值方式. 当然props 还提供许多验证,使数据更加严谨. 在使用父子传值时,出现了, 按照文档说明,例如: 1 <template> 2 <div v-if="data">{{parentName}} 3 4 <input type="text" v-model=

Vue父子组件通信实践

组件(Component)是Vue.js的核心部分,组件的作用域是孤立的,所以不能在子组件模板内直接引用父组件的数据,但是组件之间的通信是必不可少的.组件A在其模板中使用了组件B,A组件要向B组件传递数据,B组件要将其内部发生的事情告知A组件,那么A.B组件怎么进行通信呢? Vue.js父子组件的关系可以总结为props down,events up,父组件通过props向下传递数据给子组件,子组件通过events给父组件发送消息,它们的工作方式如下图所示: 父组件 - 子组件:父组件传值给子组

vue兄弟组件通信

所有需要通信的组件共用一个事件对象.new Vue(),新new一个vue对象,然后所有组件都访问这个对象,通过这个对象$emit事件,也通过这个对象$on监听事件. 实现一:直接在单文件里面实例化根Vue的时候,在data里return一个属性,值为new Vue(),例如bus: new Vue() 在具体的组件页面上,直接用this.$root.bus.$emit('eventName')发送事件. 在另一个组件页面的mounted函数里面,用this.$root.bus.$on('eve

Vue父子组件通信

我们在开发过程中,不可能只在一个组件里面写东西,当一个组件的内容过多时候,我们会把各个块的内容分别作为一个单独的组件分离出去,这些分离出去的组件被叫做子组件,但是一般请求数据的话还是在父组件(最大的外层中)钟情求,这就要求我们要将父组件中的数据传给各个子组件,子组件才能通过v-for指令进行遍历,遍历之后再通过item将数据传给比自己更小的子组件,这样以此类推,最后的小组件再来展示数据,组件开发中不可能因为某个小组件而单独发一次请求,这就需要用到组件之间的通信 通常情况下子组件是不能直接访问父组

vue中组件通信

前面看官方文档一直不能理解在子组件模板中引用父组件的数据,看了很多遍也是模糊,今天无意中看到一个demo,突然就明白了一些. <div id="componentPhone"> <!--在子组件模板中引用父组件的数据,数据是article,通过绑定的detail属性--> <my-component v-bind:detail="article"></my-component></div> var cp =

vue基础——组件(组件嵌套)

介绍 vue中页面是由组件组成的,即以.vue结尾的文件. .vue文件由三部分组成,分别是template.script.style. 分别写html.js.css代码. 组件之间可以互相嵌套.所以就形成了整个页面. 外部引用的都是一些公共的方法和样式这种. 组件嵌套 1.创建一个login组件 2.的在HelloWorld.vue中引用 export default就是可以让外部引用的意思. 原文地址:https://www.cnblogs.com/zhanzhuang/p/9241232.

vue组件通信(props,$emit,$attrs,$listeners)

朝颜陌 vue基础----组件通信(props,$emit,$attrs,$listeners) 一.父传子,子传孙 1. props 1>在父组件中通过子组件自定义的标签属性来传递数据. 2>在子组件中通过props声明希望用到的数据 1 <body> 2 <div id="app"> 3 <my-father :msg1="msg" a="10" :b="20" @click1=&