Nuxt.js: Where can I define global App.mounted hook (on a level higher than layout component)

Created on 6 Sep 2017  路  6Comments  路  Source: nuxt/nuxt.js

Is there a higher level component where I can define mounted hook function than layout? Say I have two layouts and they share same code in their mounted function, where should I extract those code out?

This question is available on Nuxt.js community (#c1422)
help-wanted

Most helpful comment

Workaround for googlers:

1) Create file utils/extend-vue-app.js:

export default function extend (app, mixin) {
    if (!app.mixins) {
        app.mixins = []
    }
    app.mixins.push(mixin)
}

2) Create plugins/foo.js:

import extend from 'utils/extend-vue-app'

export default async function ({ app }) {
    extend(app, {
        mounted () {
            console.log('Hooray, Nuxt.js app mounted.')
        },
    })
}

3) Load your new plugin in nuxt.config.js:

module.exports = {
    ...
    plugins: [
        ...
        '~/plugins/foo.js',
    ],
}

@alexchopin I use this for all kinds of things:

  • Load external scripts which will adjust DOM, for example twitter.js (because if they load before Vue app inits, this gives "The client-side rendered virtual DOM tree is not matching server-rendered content.")
  • Add global document event listeners
  • Deal with client side cookies on page load
  • Add global data to the app (e.g. for vue-stash)

All 6 comments

I think you can create a plugin or component and import in all of your layouts.

I thought about using plugin but it seems there no way to do what I wanted. The closest option is to use a mixin and define a mounted hook but that will apply to all components. If you could give me an example, that would be very appreciated.

And I don't understand component and import in all of your layouts.. Could you elaborate in more details?

Thanks

Hi @zzhjerry,
Can you give us more information about what you would like to achieve in the global mounted function?

Workaround for googlers:

1) Create file utils/extend-vue-app.js:

export default function extend (app, mixin) {
    if (!app.mixins) {
        app.mixins = []
    }
    app.mixins.push(mixin)
}

2) Create plugins/foo.js:

import extend from 'utils/extend-vue-app'

export default async function ({ app }) {
    extend(app, {
        mounted () {
            console.log('Hooray, Nuxt.js app mounted.')
        },
    })
}

3) Load your new plugin in nuxt.config.js:

module.exports = {
    ...
    plugins: [
        ...
        '~/plugins/foo.js',
    ],
}

@alexchopin I use this for all kinds of things:

  • Load external scripts which will adjust DOM, for example twitter.js (because if they load before Vue app inits, this gives "The client-side rendered virtual DOM tree is not matching server-rendered content.")
  • Add global document event listeners
  • Deal with client side cookies on page load
  • Add global data to the app (e.g. for vue-stash)

@IlyaSemenov , thanks

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bimohxh picture bimohxh  路  3Comments

vadimsg picture vadimsg  路  3Comments

gary149 picture gary149  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments

VincentLoy picture VincentLoy  路  3Comments