8

I want to rename or create an alias a Vue component exported from inside a plugin.

Let's say this is how I use the library

import VModal from "vue-js-modal";
Vue.use(VModal);

This exposes a modal component to me. And I want use it all over my app as my-modal how can this be done?

P.S. Some libraries do provide a way to rename this, but I want to be able to change it on my end due to some limitation.

3
  • when you import it. like import my-modal from 'vue-js-modal' then Vue.use(my-modal); Commented Apr 30, 2019 at 14:22
  • imported like import VModal from "vue-js-modal"; I don't think what you suggest will work. Commented Apr 30, 2019 at 14:23
  • Actually the VModal is the name you are giving to the component Commented Apr 30, 2019 at 14:24

5 Answers 5

26

The name of what you import only matters when you import something using curly braces, since you are importing specific things from that file/package/whatever.

If you don't use curly braces, you are just importing whatever is exported as default from that file/package/whatever and can therefore give it whatever name you want.

For example, something like this means "import specifically x and y from z":

import { x, y } from 'z'

These names, x and y, need to correspond with something exported in z with those names.


Something like this however is just saying "import the default thing from z and give it the alias MyThing":

import MyThing from 'z'

If you want to give a non-default import a name, you'd need to do something like this:

import { x as MyThing } from 'z'

This will import the non-default thing x and give it the alias MyThing.

Sign up to request clarification or add additional context in comments.

Comments

4

Had a similar issue just now.

I wanted to import two components with the same name but different modules as follows:

import LabelsField from '@/components/Index/LabelsField'
import LabelsField from '@/components/Form/LabelsField'

Importing default and making an alias worked fine:

import { default as LabelsFieldIndex } from '@/components/Index/LabelsField'
import LabelsField from '@/components/Form/LabelsField'

...

  components: {
    LabelsFieldIndex,
    LabelsField
  },

1 Comment

This also worked for me. However for others: if I made an alias for both of them, that did not work. It only worked when one of them did not have an alias and the other did. So it appears one has to retain the original name.
3

new Vue({ el: '#app', components: { 'component-a': ComponentA, 'component-b': ComponentB } }) for more details go follow links https://v2.vuejs.org/v2/guide/components-registration.html

Comments

2

Vue.component('my-modal', VModal )

1 Comment

This is if you want to use it as a component. By your description, I couldn't tell if you're using it as a component or as a plugin. You used the word component in your description but your code has Vue.use(), which is for plugins.
0

When you import it. Like

 import my-modal from 'vue-js-modal' 

then

Vue.use(my-modal); 

3 Comments

have you tried this?
Yes, but it is for Vue2, I don't think it will work for vue3
Imports with - won't work in Javascript in general, "myModal" will.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.