Vue 初体验 (十) 混合 mixins 和 插槽slots

先来看混合 mixins

未操作前
在这里插入图片描述

鼠标移到bys上

在这里插入图片描述

点击按钮

在这里插入图片描述

在这两个组件中,存在着重复的代码,通过mixins 减少代码工作量

html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <!-- Vue  混合 mixins -->
<div id="app">
<tooltip></tooltip>
<popup></popup>
</div>

<template id="Popuptemp">
<div>
  <button @click="toggle">Popup</button>
  <div v-if="visible">
    <span @click="hide">关闭</span>
    <h4>标题</h4>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore hic maxime excepturi nisi, dolor officia quos soluta et, quod! Exercitationem nostrum quo ut quae, tempora eaque! Repellendus, temporibus vel nisi.
  </div>
</div>
</template>

<script src="vue.js"></script>
<script src="main.js"></script>
</body>
</html>

JS

var base={
  methods:{
    show:function(){
      this.visible=true;
    },
    hide:function(){
      this.visible=false;
    },
    toggle:function(){
      this.visible=!this.visible;
    },
  },
  data:function(){
    return {
      visible:false,
    }
  }
}

Vue.component('tooltip',{
  template:'<div><span @mouseenter="show" @mouseleave="hide">bys</span><div v-if="visible">白岩松</div></div>',
  mixins:[base],

});

Vue.component('popup',{
    template:'#Popuptemp',
    mixins:[base],
    //设置默认属性,会覆盖mixins中的默认属性
    data:function(){
      return {
        visible:false,
      }
    }
});


new Vue({
  el:'#app',
  data:{

  }
})

再来看插槽slots

效果图 文字通过html自定义显示

在这里插入图片描述

HTML

<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .panel  {
      max-width:300px;
      border:1px solid #999;
      margin-bottom:15px;
    }
    .panel > * {
        padding:15px;
      }
    .panel  .title{
      border-bottom:1px solid #999;
    }

    .panel  .footer{
      border-top:1px solid #999;
    }

  </style>
</head>
<body>
  <!-- Vue 插槽slots-->
<div id="app">
<panel>
  <div slot="title">我第一帅</div>
  <div slot="content">hahahahaha</div>
  <div slot="footer">我滴脚</div>
</panel>

<panel>
  <div slot="title">我第一帅</div>
  <div slot="content">hahahahaha</div>
</panel>

</div>

<template id="panel-tpl">
  <div class="panel">
    <div class="title">
      <slot name="title"></slot>
    </div>
    <div class="content">
      <slot name="content"></slot>
    </div>
    <div class="footer">
        <slot name="footer">默认页脚</slot>
    </div>
  </div>
</template>

<script src="vue.js"></script>
<script src="main.js"></script>
</body>
</html>

JS

Vue.component('panel',{
  template:'#panel-tpl',
})


new Vue({
  el:'#app',
  data:{}
})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzm996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值