14
votes

I'm using vue-cli for build my lib with this command:

"build": "vue-cli-service build --target lib --name myLib ./src/component.vue"

How can I import my component from the dist folder after the build?

If I import from path-to-myLib/src/component.vue, everything is fine! But the code below does not work:

// undefined
import { component } from 'path-to-myLib/dist/myLib.umd.js' 
// undefined
import myComponent'path-to-myLib/dist/myLib.umd.js' 
// result: https://i.stack.imgur.com/xHSzL.png
import * as myComponent'path-to-myLib/dist/myLib.umd.js'

I cannot understand what the problem is.

2
In order to import a library it has to be exported from your main.js or the mylib.umd.js first. You should include (import) the .vue component inside that file and then export it from there. Check my answer one how to import vue components here: stackoverflow.com/questions/47754244/…samayo
Try export { foobar }samayo
Did u give your components a name? It is required. Component.vue should have name attribute with value of the component you are trying to importsamayo
I meant inside your component.vue like in this example github.com/samayo/vuejs-hello-app/blob/master/src/components/…. If not check my previous link or the github code, the SO link shows step-by-step process on how to make the lib and the github code shows the finished product. You can mirror-check your code to see what is missingsamayo
Ah, sorry for that. Wish I could help but I had the same issue when building the library so your problem is not that far apartsamayo

2 Answers

0
votes

Wherever you'd like to use your built component, just import it like this:

import MyComponent from 'path/to/MyComponent.umd.js'

This syntax was not mentioned in your attempts, so I guess it is just an import statement typo.

0
votes

you must add main to your package.json like this "main":"path-to-myLib/dist/myLib.umd.js", pointing to the generated umd file and then use import {component} from 'library' and use in library library name