美文网首页
Vue包装第三方组件

Vue包装第三方组件

作者: seaasun | 来源:发表于2019-04-15 17:58 被阅读0次

有个第三方组件Third-Component, 你想包装再利用,可以如下写:

<Third-Component v-bind="$attrs" v-on="$listeners">
    <template v-for="(_, slot) of $scopedSlots" v-slot:[slot]="scope">
      <slot :name="slot" v-bind="scope"/>
    </template>
    <slot/>
</Third-Component>

例子

比如你常用UI组件库中的弹框组件Modal,每次调用时需将其width设为1000:

<Modal width="1000">
 // ...
</Modal

你不想每次调用都写width="1000" ,因为可能以后需改为width=800,你希望width="1000" 只出现一次。
你可以创建一个新组件my-modal,组件内容如下:

<template>
  <Modal 
         v-bind="$attrs"  
         v-on="$listeners"
         :width="1000">
    <template v-for="(_, slot) of $scopedSlots" v-slot:[slot]="scope">
      <slot :name="slot" v-bind="scope"/>
    </template>
    <slot/>
  </Modal>
</template>

<script>
  export default {
    name: "myModal",
  }
</script>

以后你只需调用<my-modal>即可。在My-Modal中可以用Modal的全部props,事件监听和slot。

分析

v-bind="$attrs" 

传递所有的prop

v-on="$listeners"

传递所有的监听

<template v-for="(_, slot) of $scopedSlots" v-slot:[slot]="scope">
      <slot :name="slot" v-bind="scope"/>
</template>

遍历并写入所有的slot插槽

相关文章

  • Vue中scoped穿透 修改子组件样式

    vue引用了第三方组件(如使用vue.ant.design的组件时),需要在组件中局部修改第三方组件的样式,而又不...

  • Vue包装第三方组件

    有个第三方组件Third-Component, 你想包装再利用,可以如下写: 例子 比如你常用UI组件库中的弹框...

  • VUE项目中修改第三方组件样式问题

    通过脚手架搭建的项目(VUE)引用第三方组件,需要在组件中局部修改第三方组件的样式 本组件最外层 >>> 第三方组...

  • Element-UI 相关文章收藏

    【Vue】 element ui 引入第三方图标vue组件样式添加scoped属性之后,无法被父组件修改。或者无法...

  • vue2.x与vue3.x的对比

    异步组件(vue3.x新增) vue3.x 由于函数式组件被定义为纯函数,因此异步组件的定义需要通过将其包装在新的...

  • XiaoShang Replay

    新增加的组件 表单元素和Vuex c-accordion 新使用的第三方组件 portal-vue vue-vir...

  • vue css >>> /deep/ 穿透

    vue引用了第三方组件,需要在组件中局部修改第三方组件的样式,而又不想去除scoped属性造成组件之间的样式污染。...

  • vue css >>> /deep/ 穿透

    vue引用了第三方组件,需要在组件中局部修改第三方组件的样式,而又不想去除scoped属性造成组件之间的样式污染。...

  • vue修改第三方组件样式

    vue引用了第三方组件,需要在组件中局部修改第三方组件的样式,而又不想去除scoped属性造成组件之间的样式污染。...

  • vue抽屉组件

    vue-drawer 介绍 vue组件:抽屉现在许多第三方组件库都非常完善,如element-ui,但缺少一个重要...

网友评论

      本文标题:Vue包装第三方组件

      本文链接:https://www.haomeiwen.com/subject/terpwqtx.html