千峰商城-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">&nbsp;{{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>

 

 

 

 
posted @ 2022-07-11 17:39  临易  阅读(18)  评论(0编辑  收藏  举报