Access Nuxt plugins in .js files










1















Let's say that I have a script file, foo.js:



function doStuff() 
// how to access store and other plugins here?


export default doStuff


Without passing the calling component as an argument, how can I access things like app or installed plugins like store, i18n in a script file like the one above?










share|improve this question






















  • Please, elaborate why default plugin format doesn't work for you? export default ( app, store ) => /* plugin code */

    – aBiscuit
    Nov 14 '18 at 15:33











  • @aBiscuit Because the script file is not a plugin

    – Johan
    Nov 14 '18 at 15:41











  • Where does doStuff is expected to be called from? Components, store or other places? This may help to determine better approach of implementation.

    – aBiscuit
    Nov 14 '18 at 17:00











  • @aBiscuit Sorry for not being clear about that. Mainly from components. I would like to avoid doStuff(this), doStuff.call(this) etc.

    – Johan
    Nov 14 '18 at 18:56















1















Let's say that I have a script file, foo.js:



function doStuff() 
// how to access store and other plugins here?


export default doStuff


Without passing the calling component as an argument, how can I access things like app or installed plugins like store, i18n in a script file like the one above?










share|improve this question






















  • Please, elaborate why default plugin format doesn't work for you? export default ( app, store ) => /* plugin code */

    – aBiscuit
    Nov 14 '18 at 15:33











  • @aBiscuit Because the script file is not a plugin

    – Johan
    Nov 14 '18 at 15:41











  • Where does doStuff is expected to be called from? Components, store or other places? This may help to determine better approach of implementation.

    – aBiscuit
    Nov 14 '18 at 17:00











  • @aBiscuit Sorry for not being clear about that. Mainly from components. I would like to avoid doStuff(this), doStuff.call(this) etc.

    – Johan
    Nov 14 '18 at 18:56













1












1








1


1






Let's say that I have a script file, foo.js:



function doStuff() 
// how to access store and other plugins here?


export default doStuff


Without passing the calling component as an argument, how can I access things like app or installed plugins like store, i18n in a script file like the one above?










share|improve this question














Let's say that I have a script file, foo.js:



function doStuff() 
// how to access store and other plugins here?


export default doStuff


Without passing the calling component as an argument, how can I access things like app or installed plugins like store, i18n in a script file like the one above?







javascript vue.js nuxt






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:48









JohanJohan

16.3k38141245




16.3k38141245












  • Please, elaborate why default plugin format doesn't work for you? export default ( app, store ) => /* plugin code */

    – aBiscuit
    Nov 14 '18 at 15:33











  • @aBiscuit Because the script file is not a plugin

    – Johan
    Nov 14 '18 at 15:41











  • Where does doStuff is expected to be called from? Components, store or other places? This may help to determine better approach of implementation.

    – aBiscuit
    Nov 14 '18 at 17:00











  • @aBiscuit Sorry for not being clear about that. Mainly from components. I would like to avoid doStuff(this), doStuff.call(this) etc.

    – Johan
    Nov 14 '18 at 18:56

















  • Please, elaborate why default plugin format doesn't work for you? export default ( app, store ) => /* plugin code */

    – aBiscuit
    Nov 14 '18 at 15:33











  • @aBiscuit Because the script file is not a plugin

    – Johan
    Nov 14 '18 at 15:41











  • Where does doStuff is expected to be called from? Components, store or other places? This may help to determine better approach of implementation.

    – aBiscuit
    Nov 14 '18 at 17:00











  • @aBiscuit Sorry for not being clear about that. Mainly from components. I would like to avoid doStuff(this), doStuff.call(this) etc.

    – Johan
    Nov 14 '18 at 18:56
















Please, elaborate why default plugin format doesn't work for you? export default ( app, store ) => /* plugin code */

– aBiscuit
Nov 14 '18 at 15:33





Please, elaborate why default plugin format doesn't work for you? export default ( app, store ) => /* plugin code */

– aBiscuit
Nov 14 '18 at 15:33













@aBiscuit Because the script file is not a plugin

– Johan
Nov 14 '18 at 15:41





@aBiscuit Because the script file is not a plugin

– Johan
Nov 14 '18 at 15:41













Where does doStuff is expected to be called from? Components, store or other places? This may help to determine better approach of implementation.

– aBiscuit
Nov 14 '18 at 17:00





Where does doStuff is expected to be called from? Components, store or other places? This may help to determine better approach of implementation.

– aBiscuit
Nov 14 '18 at 17:00













@aBiscuit Sorry for not being clear about that. Mainly from components. I would like to avoid doStuff(this), doStuff.call(this) etc.

– Johan
Nov 14 '18 at 18:56





@aBiscuit Sorry for not being clear about that. Mainly from components. I would like to avoid doStuff(this), doStuff.call(this) etc.

– Johan
Nov 14 '18 at 18:56












1 Answer
1






active

oldest

votes


















1














There are multiple ways to call custom function with this being a reference to the component it was invoked in.



1) Using mixins



Custom function can be declared as a method and used within component via this.customMethod.



customHelpers.js



const customHelpers = 
methods:
doStuff ()
// this will be referenced to component it is executed in





component.vue



// component.vue
import customHelpers from '~/mixins/customHelpers'
export default
mixins: [customHelpers],
mounted ()
this.doStuff()




2. Using context injection



Declare custom plugin:



plugins/customHelpers.js



import Vue from 'vue'

Vue.prototype.$doStuff = () => /* stuff happens here */


And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



This makes method available inside every component:



export default 
mounted ()
this.$doStuff()




3) Using combined injection



Same as method 2, but injection will be also accessible inside fetch, asyncData and inside store modules. Bindings to this may vary, since context is not available everywhere.



plugins/customHelpers.js



export default ( app , inject) => 
inject('doStuff', () => /* stuff happens here */ )



And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



Usage example:



export default 
asyncData ( app )
app.$doStuff()




Please, refer to documentation for more examples.






share|improve this answer























  • Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

    – Johan
    Nov 14 '18 at 20:04











  • Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

    – aBiscuit
    Nov 14 '18 at 20:41










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302905%2faccess-nuxt-plugins-in-js-files%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There are multiple ways to call custom function with this being a reference to the component it was invoked in.



1) Using mixins



Custom function can be declared as a method and used within component via this.customMethod.



customHelpers.js



const customHelpers = 
methods:
doStuff ()
// this will be referenced to component it is executed in





component.vue



// component.vue
import customHelpers from '~/mixins/customHelpers'
export default
mixins: [customHelpers],
mounted ()
this.doStuff()




2. Using context injection



Declare custom plugin:



plugins/customHelpers.js



import Vue from 'vue'

Vue.prototype.$doStuff = () => /* stuff happens here */


And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



This makes method available inside every component:



export default 
mounted ()
this.$doStuff()




3) Using combined injection



Same as method 2, but injection will be also accessible inside fetch, asyncData and inside store modules. Bindings to this may vary, since context is not available everywhere.



plugins/customHelpers.js



export default ( app , inject) => 
inject('doStuff', () => /* stuff happens here */ )



And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



Usage example:



export default 
asyncData ( app )
app.$doStuff()




Please, refer to documentation for more examples.






share|improve this answer























  • Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

    – Johan
    Nov 14 '18 at 20:04











  • Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

    – aBiscuit
    Nov 14 '18 at 20:41















1














There are multiple ways to call custom function with this being a reference to the component it was invoked in.



1) Using mixins



Custom function can be declared as a method and used within component via this.customMethod.



customHelpers.js



const customHelpers = 
methods:
doStuff ()
// this will be referenced to component it is executed in





component.vue



// component.vue
import customHelpers from '~/mixins/customHelpers'
export default
mixins: [customHelpers],
mounted ()
this.doStuff()




2. Using context injection



Declare custom plugin:



plugins/customHelpers.js



import Vue from 'vue'

Vue.prototype.$doStuff = () => /* stuff happens here */


And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



This makes method available inside every component:



export default 
mounted ()
this.$doStuff()




3) Using combined injection



Same as method 2, but injection will be also accessible inside fetch, asyncData and inside store modules. Bindings to this may vary, since context is not available everywhere.



plugins/customHelpers.js



export default ( app , inject) => 
inject('doStuff', () => /* stuff happens here */ )



And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



Usage example:



export default 
asyncData ( app )
app.$doStuff()




Please, refer to documentation for more examples.






share|improve this answer























  • Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

    – Johan
    Nov 14 '18 at 20:04











  • Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

    – aBiscuit
    Nov 14 '18 at 20:41













1












1








1







There are multiple ways to call custom function with this being a reference to the component it was invoked in.



1) Using mixins



Custom function can be declared as a method and used within component via this.customMethod.



customHelpers.js



const customHelpers = 
methods:
doStuff ()
// this will be referenced to component it is executed in





component.vue



// component.vue
import customHelpers from '~/mixins/customHelpers'
export default
mixins: [customHelpers],
mounted ()
this.doStuff()




2. Using context injection



Declare custom plugin:



plugins/customHelpers.js



import Vue from 'vue'

Vue.prototype.$doStuff = () => /* stuff happens here */


And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



This makes method available inside every component:



export default 
mounted ()
this.$doStuff()




3) Using combined injection



Same as method 2, but injection will be also accessible inside fetch, asyncData and inside store modules. Bindings to this may vary, since context is not available everywhere.



plugins/customHelpers.js



export default ( app , inject) => 
inject('doStuff', () => /* stuff happens here */ )



And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



Usage example:



export default 
asyncData ( app )
app.$doStuff()




Please, refer to documentation for more examples.






share|improve this answer













There are multiple ways to call custom function with this being a reference to the component it was invoked in.



1) Using mixins



Custom function can be declared as a method and used within component via this.customMethod.



customHelpers.js



const customHelpers = 
methods:
doStuff ()
// this will be referenced to component it is executed in





component.vue



// component.vue
import customHelpers from '~/mixins/customHelpers'
export default
mixins: [customHelpers],
mounted ()
this.doStuff()




2. Using context injection



Declare custom plugin:



plugins/customHelpers.js



import Vue from 'vue'

Vue.prototype.$doStuff = () => /* stuff happens here */


And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



This makes method available inside every component:



export default 
mounted ()
this.$doStuff()




3) Using combined injection



Same as method 2, but injection will be also accessible inside fetch, asyncData and inside store modules. Bindings to this may vary, since context is not available everywhere.



plugins/customHelpers.js



export default ( app , inject) => 
inject('doStuff', () => /* stuff happens here */ )



And use plugin in nuxt.config.js



export default 
..., // other nuxt options
plugins: ['~/plugins/customHelpers.js']



Usage example:



export default 
asyncData ( app )
app.$doStuff()




Please, refer to documentation for more examples.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 19:37









aBiscuitaBiscuit

1,6621616




1,6621616












  • Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

    – Johan
    Nov 14 '18 at 20:04











  • Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

    – aBiscuit
    Nov 14 '18 at 20:41

















  • Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

    – Johan
    Nov 14 '18 at 20:04











  • Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

    – aBiscuit
    Nov 14 '18 at 20:41
















Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

– Johan
Nov 14 '18 at 20:04





Thanks for your examples. Let's say that I don't want to define a plugin or mixin, and simply want to access the store or similar inside customHelpers.js. Is that possible?

– Johan
Nov 14 '18 at 20:04













Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

– aBiscuit
Nov 14 '18 at 20:41





Taking a step back.. Generally, what you want to do is to have a function with dynamically bound context. There has to be an access point for context. It can be done right in place of invocation (e.g. doStuff.bind(this), or assigning function directly to component's method, so Vue does binding for you), or it can be done through ways provided by environment you develop in - Nuxt.js in this case, which are listed above. I don't think there would be better options in terms of keeping logic maintainable and predictable.

– aBiscuit
Nov 14 '18 at 20:41



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302905%2faccess-nuxt-plugins-in-js-files%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20