千峰商城-springboot项目搭建-37-vue组件插槽使用

当我们自定义vue组件时,允许组件中的部分内容在调用组件时进行自定义。————插槽。

 

 

1.js中定义插槽:

 在自定义组件中通过<slot>标签在模板中定义插槽。

//定义一个header-bar组件
Vue.component("header-bar",{
    data:function(){
        //组件中的data是通过函数返回的对象
        return{
            //title:"java2022电商平台"
            str2:"子组件中的数据"
        };
    },
    template:`<div class="divStyle">
                <table class="tableStyle">
                    <tr>
                        <td width="200px" align="right" valign="middle">
                            <img src="img/logo.png" class="logoImg" />
                        </td>
                        <td>
                            <label class="titleStyle">
                            {{title}}
                            </label>
                        </td>
                        <td>
                            <!--定义一个插槽-->
                            <slot></slot>
                        </td>
                        <td>
                            <button @click="childMethod">子组件中的按钮</button>
                        </td>
                    </tr>
                </table>
            </div>`,
            props:["title"],
            methods:{
                childMethod:function(){
                    //执行父组件中的方法
                    this.$emit("my-event",this.str2);
                }
            }
            });

 

 2.页面1:
在父组件中调用此组件时,可以指定插槽填充的模板。
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.css" />
        <link rel="stylesheet" href="css/my-components.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>
        <style type="text/css">
            a{
                color: darkblue;
                font-size: 18px;
                background: white;
                border: blueviolet 1px solid;
                padding: 3px 5px;
                text-decoration: none;
                border-radius: 3px;
            }
            a:hover{
                background: wheat;
                color: red;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <!--使用header-bar组件-->
            <header-bar :title="str">
                <div>
                    <a href="#">首页</a>
                    <a href="#">首页</a>
                    <a href="#">首页</a>
                    <a href="#">首页</a>
                </div>
            </header-bar>
                            
        </div>
        
        <script type="text/javascript" src="js/my-components.js"></script>
        <script type="text/javascript">            
            var vm = new Vue({
                el:"#container",
                data:{
                    str:"java2022千峰武汉电商平台",
                    str3:""
                },
                methods:{
                    parentMethod:function(p){
                        vm.str3 = p;
                    }
                }
            });
        </script>        
    </body>
</html>

 

 

 

 

 

 3.页面2:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/my-components.css" />
    </head>
    <body>
        <div id="container">
            <!--使用header-bar组件-->
            <!--组件的引用必须在vue实例指定的容器中-->
            <header-bar :title="str" @my-event="parentMethod">
                <input /><button>搜索</button>
            </header-bar>    
            从子组件传递的数据:{{str3}}
        </div>
        
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/my-components.js"></script>
        <script type="text/javascript">    
            //vue实例本身就是一个组件。(模板就是el指定的容器div,data就是组件数据,methods就是组件的事件函数)
            //在vue实例指定的el容器中引用的组件<header-bar>称为子组件,当前Vue实例就是父组件
            var vm = new Vue({
                el:"#container",
                data:{
                    str:"java2022千峰武汉电商平台",
                    str3:""
                },
                methods:{
                    parentMethod:function(p){
                        vm.str3 = p;
                    }
                }
            });
        </script>
        
    </body>
</html>

 

 

 

posted @ 2022-07-11 15:55  临易  阅读(35)  评论(0编辑  收藏  举报