1

I am using Vue plugins so that user can access a global component once registering the global component and configuring it inside Vue.use. For this I need to pass some data from Vue.use() to Component.vue.

Take a look at the following code:

Vue.use(MyPlugin, { data: true });

the code of MyPlugin is

import Plugin from './plugin';

const IconPlugin = {
  install(Vue, options) {
    console.log(options); // Here I can see {data: true}
    Vue.component('GlobalComponent', Icon);
  },
};

Now I need to pass this options variable to the component. So that a user whenever use

<GlobalComponent />

{data: true} should always be there.

Basically, that is a configuration which user is passing and the further component computation will be dependent on this.

2 Answers 2

3

You can use Vue.extend to extend components

var Icon = Vue.extend({
  data: function() {
    return {
      foo: 'fooooo',
      bar: 'barr'
    }
  },
  template: '<div><button @click="doFooStuff">{{foo}}</button><button @click="doBarStuff">{{bar}}</button></div>',
  methods: {
    doBarStuff: function() {
      console.log(this.foo, this.bar)
    },
    doFooStuff: function() {
      console.log(this.foo)
    }
  }
})


const IconPlugin = {
  install(Vue, options) {
    // console.log(options);
    // normalize data (select only props you want)
    var data = Object.assign({}, options);
    var NewIcon = Icon.extend({
      data: function() {
        return data;
      }
    })
    Vue.component('GlobalComponent', NewIcon);
  },
};

Vue.use(IconPlugin, {
  foo: 'FOOO'
});


new Vue({
  el: '#app',
  components: {
    Icon
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
  <icon></icon>
  <global-component></global-component>
</div>

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

Comments

0

it sounds like you want to take a look at the component guide. It would seem that you want to merge the data with where you are passing Icon.

1 Comment

This should rather be a comment.. But yes i want to pass options in Vue.component method

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.