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

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 千峰商城-springboot项目搭建-37-vue组件插槽使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

当我们自定义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>

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

 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>

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128138.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!