vue组件的封装

第一步:封装一个自动全局注册组件的逻辑

思路:定义三种组件类型,根据文件后缀去判断是那种类型,该如何注册

// 获取components目录下所有index文件
const files = require.context(
  '../components', // 获取这个目录下所有文件
  true, // 是否遍历子目录
  /^\.\/(\w*\/)+index\.(vue|js)$/
)
export default {
  install(Vue) {
    files.keys().forEach(path => {
      // console.log(this.fileType())
      const filetype = this.fileType(path, files(path))
      if (filetype === 'component') {
        this.useComponent(Vue, files(path))
      } else if (filetype === 'componentGroup') {
        this.useComponentGroup(Vue, files(path))
      } else {
        this.useServeApi(Vue, files(path))
      }
    })
  },
  // 类型判断  定义三种类型
  fileType(path, file) {
    if (path.lastIndexOf('.js') === -1) {
      return 'component'
    } else {
      if (file.default.install) {
        return 'serveApi'
      } else {
        return 'componentGroup'
      }
    }
  },
  // 普通组件
  useComponent(Vue, file) {
    const options = file.default
    const name = options.name
    Vue.component('md' + name, options)
  },
  // 组件api
  useServeApi(Vue, file) {
    Vue.use(file.default)
  },
  // 嵌套组件
  useComponentGroup(Vue, file) {
    const components = file.default
    for (const key in components) {
      const name = components[key].name
      Vue.component('md' + name, components[key])
    }
  }
}

// main.js 
import baseComponent from './plugins/baseComponent'
Vue.use(baseComponent)
// 这样的话就会自动全局注册,不用每个页面都去引用组件,也不用一个一个在main.js里面注册

然后就是组件的封装了

举个简单的列子,我在这里封装一个button

// components/Button/index.vue  这就是个普通组件

<template>
  <button class="btn" :class="newStyle" @click='getdata'>
    <slot></slot>
  </button>
</template>

<script>
export default {
  name: 'Button',
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    newStyle () {
      return {
        disabled: this.disabled
      }
    }
  },
  methods: {
    getdata() {
      this.$emit('click')
    }
  }
}
</script>

<style lang="scss" scoped>
.btn{
  width: 200px;
  height: 45px;
  border: 1px solid #ccc;
  background-color: #fff;
  &.disabled{
    opacity: 0.5;
    background-color: #ccc;
  }
}
</style>

// 在其他页面就可以直接调用了
<md-button @click="submint" disabled='true'>保存地址</md-button>

我定义的三个组件类型:index.vue就是普通组件,index.js就是api组件,多个vue文件就是嵌套组件

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue组件封装和复用是指将代码逻辑和功能封装在一个独立的组件中,并在需要的地方重复使用该组件的过程。 Vue组件封装可以通过以下步骤实现: 1. 创建组件:使用Vue框架提供的组件选项来创建一个组件。可以使用Vue.extend方法或者直接在Vue实例中定义一个组件。 2. 封装功能:在组件中添加业务逻辑、数据和方法等功能。可以通过计算属性、监听器、方法等实现具体的功能。 3. 编写模板:使用Vue的模板语法编写组件的结构和样式。通过将标签、属性和事件绑定到组件的数据和方法来实现交互效果。 4. 注册组件:将组件注册到Vue实例中,使其可以在其他组件中使用。可以使用Vue.component方法全局注册组件,也可以在局部组件中通过components选项注册组件。 5. 使用组件:在需要的地方使用组件,可以通过标签的方式将组件插入到页面中。 通过封装组件,可以将代码逻辑和UI元素进行有效地拆分和复用。例如,可以将页面中重复出现的按钮、表单、卡片等元素封装组件,通过复用组件来提高代码的可维护性和复用性。同时,组件化的思想也使得团队协作更加高效,不同开发者可以独立开发、测试和维护自己负责的组件,最终组合成完整的应用程序。 总之,Vue组件封装和复用是一种有效的开发方式,可以提高代码的可维护性和可复用性,同时也促进了团队协作和开发效率的提升。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值