您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

vue中父子组件传值及slot使用剖析

2024/3/17 18:01:00发布18次查看
这次给大家带来vue中父子组件传值及slot使用剖析,vue中父子组件传值及slot使用的注意事项有哪些,下面就是实战案例,一起来看一下。
一.父子组件传值
<!doctype html> <html lang="en"> <head>   <meta charset="utf-8">   <title>父子组件传值</title>   <style>    </style>   <script src="./vue.js"></script> </head> <body>   <p id="root">      <counter :count="0" @numberchange="handlechange"></counter>     <counter :count="0" @numberchange="handlechange"></counter>     <p>{{total}}</p>      <validate-content content="hello world"></validate-content>   </p>   <script>     //父组件向子组件传值用 props ,加:号后传递的为js表达式,示例中则为数字,不加:号代表的是字符串     var counter = { //局部注册       props:['count'],      data:function(){//在子组件中定义数据,data不能是对象,必须是一个函数。        return {          number:this.count        }      },      template:'<p @click="handleclick2">{{number}}</p>',      methods:{       handleclick2:function(){         this.number ++;         //this.count++; 父组件可以传值给子组件,但子组件不可以修改父组件属性,这里这么写会报错。         this.$emit(numberchange,this.number);//子组件向父组件传递事件,值       }     }     }    var validatecontent = {     props:{       //content:[number,string] //组件参数校验,可以多选       content:{//组件参数校验         type:string,         required:true,         default:default value,         validator:function(value){           return value.length > 5         }       }      },      template:'<p >{{content}}</p>',    }    var vm = new vue({      el:'#root',      data:{        total:0      },      methods:{        handlechange:function(number){          console.log(number)         // this.total +=1;       }      },      components:{        counter, //局部注册要在根节点注册组件        validatecontent      }    })   </script> </body> </html>
二.父组件向子组件传递dom
先看一个示例
<body>   <p id="root">      <child><p>qin</p></child>   </p>   <script>     let child = {      template :`<p>            <p>hello world</p>          </p>`    }    var vm = new vue({      el:'#root',      components:{        child      }     })   </script> </body>
打开查看器查看一下
发现qin不见了
<p>qin</p>1
查看官方文档 ,   https://cn.vuejs.org/v2/guide/components-slots.html
我们得出结论:如果 child 没有包含一个 < slot > 元素,则任何传入它的内容都会被抛弃
我们加入插槽
<body>   <p id="root">      <child><p>qin</p></child>   </p>   <script>     let child = {      template :`<p>            <p>hello world</p>            <slot></slot>         </p>`     }    var vm = new vue({      el:'#root',      components:{        child      }     })   </script> </body>
发现qin能正常显示,且slot将会被替换为解析后的片段 < p > qin < /p >
当父组件不向子组件传值的时候,slot还可以作为父组件默认值出现
<body>   <p id="root">      <child></child>   </p>   <script>     let child = {      template :`<p>            <p>hello world</p>            <slot>default value</slot>         </p>`    }    var vm = new vue({      el:'#root',      components:{        child      }     })   </script> </body>
效果图
具名插槽
如果想使用多个插槽,我们先看看效果:
<body>   <p id="root">      <child>       <header>this is header</header>       <footer>this is footer</footer>      </child>   </p>   <script>     let child = {      template :        `<p>             <slot></slot>            <p>main content</p>            <slot></slot>         </p>`    }    var vm = new vue({      el:'#root',      components:{        child      }     })   </script> </body>
发现出现了多个header和footer,要解决这个问题就要用到具名插槽
我们修改代码如下:
<body>   <p id="root">      <child>       <header slot="header">this is header</header>       <footer slot="footer">this is footer</footer>      </child>   </p>   <script>     let child = {      template :        `<p>             <slot name="header"></slot>            <p>main content</p>            <slot name="footer"></slot>         </p>`    }    var vm = new vue({      el:'#root',      components:{        child      }     })   </script> </body>
可以看到显示正常了
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
如何使用veevalidate在vue项目内进行表单校验功能
怎样使用vue实现2048小游戏
以上就是vue中父子组件传值及slot使用剖析的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product