Vue ui组件封装

2019-09-16  本文已影响0人  GongZH丶

开发过程中 Vue 的组件主要分为三类https://blog.csdn.net/weixin_44260504/article/details/89555563

组件封装(独立组件)

通过vue-cli搭建的项目,目录下都有components目录,自定义组件一般放在这里。


image.png

编写一个.vue文件和.js文件
index.vue内容如下:

<template>
  <div class="test_button">
    <button>
      hahaha
    </button>
  </div>
</template>
 
<script>
export default {
  name: 'TestButton' // 注意这里的name命名,就是你以后封装好后使用的组件名
}
</script>

<style lang="scss" scoped>
.test_button {
  position: relative;
  width: 300px;
  height: 300px;
  background-color: burlywood;
}
</style>

index.js内容如下:

import TestButton from "./index.vue";
const testButton = {
  install: function(Vue) {
    // Vue.component()是用来注册全局组件的
    Vue.component(TestButton.name, TestButton);
  }
};
export default testButton;
组件编写完成后,在main.js中引入 image.png

这种方式引入的是全局组件。

上一篇 下一篇

猜你喜欢

热点阅读