千峰商城-springboot项目搭建-39-vue插槽作用域
1.组件定义:
定义组件时,将组件中的数据绑定到slot标签。
Vue.component("page-frame",{ template:`<div> <div id="header" style="width:100%; height:100px; background:pink"> <slot name="s1"></slot> </div> <div style="width:100%; height:510px"> <slot name="s2" v-bind:musics="songs"></slot> </div> <div id="footer" style="width:100%; height:40px; background:lightgray">{{cr}}</div> </div>`, props:["title","cr"], data:function(){ return{ songs:[ { "id":001, "name":"逆战", "artists":[ { "id":01, "name":"张杰" } ] }, { "id":002, "name":"隐形", "artists":[ { "id":02, "name":"张韶涵" } ] } ] }; } })
2.引用组件时,在填充插槽的模板上使用slot-scopt属性获取插槽绑定的值。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="css/bootstrap.css" /> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="js/bootstrap.js"></script> <script type="text/javascript" src="js/vue.js"></script> </head> <body> <div id="container"> <page-frame title="标题" cr="千峰武汉"> <template slot="s1"> <input type="text"/><button type="button">搜索</button> </template> <!--在使用模板填充组件插槽时,可以使用slot-scope属性获取组件插槽绑定的数据--> <template slot="s2" slot-scope="res"> <table class="table table-bordered table-condensed"> <tr> <th>序号</th> <th>歌曲id</th> <th>歌名</th> <th>歌手</th> <th>操作</th> </tr> <tr v-for="song,index in res.musics"> <td>{{index+1}}</td> <td>{{song.id}}</td> <td> {{song.name}} </td> <td> <span v-for="artist in song.artists"> {{artist.name}}</span> </td> <td width="18%"> <button type="button" class="btn btn-primary btn-xs">播放</button> </td> </tr> </table> </template> </page-frame> </div> <script type="text/javascript" src="js/vue.js" ></script> <script type="text/javascript" src="js/my-components.js" ></script> <script type="text/javascript"> var vm = new Vue({ el:"#container" }); </script> </body> </html>
分类:
千峰商城项目
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性