4

I'm building my external vue component library and published to npm. My components extends Element UI components.

I followed the steps in this article and so far it works fine. https://medium.com/justfrontendthings/how-to-create-and-publish-your-own-vuejs-component-library-on-npm-using-vue-cli-28e60943eed3

npm package

https://www.npmjs.com/package/@bochen/example-lib

bundle script

"build-bundle": "vue-cli-service build --target lib --name exampleLib ./src/components/index.js",

src/components/index.js

import Vue from 'vue';
import BcButton from './Button.vue';

const Components = {
  BcButton,
};

Object.keys(Components).forEach((name) => {
  Vue.component(name, Components[name]);
});

export default Components;

src/components/Button.vue

<template>
  <el-button v-bind="$props"
             :class="{
               'button-full': full,
             }"
  >
    <slot/>
  </el-button>
</template>

<script>
import { Button } from 'element-ui';

export default {
  name: 'BcButton',
  extends: Button,
  props: {
    full: {
      type: Boolean,
      default: false,
    },
  },
};
</script>

<style scoped>
  .button-full {
    width: 100%;
  }
</style>

Issue

The problem is after I installed my library at another project, I still need to import Element UI in addition to make it work.

main.js

import Vue from 'vue'
import App from './App.vue'
import Element from 'element-ui';
import ExampleLib from '@bochen/example-lib';

import 'element-ui/lib/theme-chalk/index.css';
import '@bochen/example-lib/dist/exampleLib.css';

Vue.use(Element);

Object.keys(ExampleLib).forEach((name) => {
  Vue.component(name, ExampleLib[name]);
});

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

Is there any possible, I could just import my library and work as I expected?

chenpion
  • 41
  • 6

0 Answers0