Search code examples
vue.jsnuxt.jsmonkeypatching

Where to put 'monkeypatches' in Nuxt.js?


where can I put 'monkeypatches' and 'my extensions' to prototypes of basic javascript objects in Nuxt framework to have it's functionality accesible accros all files ?

for example:

String.prototype.capitalize = function () {

    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

Solution

  • Hello you can create a ~/plugins/my-extensions.js file with your code sample. This code will be run once (server-side and client-side as well) and will make available your extensions methods everywhere.

    Do not forget to register your plugin in nuxt.config.js:

    export default {
      plugins: ['~/plugins/my-extensions.js']
    }