Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I access a method in a different page in Nuxt #3221

Closed
Myolisi opened this issue Apr 13, 2018 · 4 comments
Closed

How can I access a method in a different page in Nuxt #3221

Myolisi opened this issue Apr 13, 2018 · 4 comments
Labels

Comments

@Myolisi
Copy link

Myolisi commented Apr 13, 2018

Am trying to implement an update function for my app using axios, now how can I access this function from all my pages in the app?

This question is available on Nuxt.js community (#c6898)
@ghost ghost closed this as completed Apr 13, 2018
@ghost ghost added the cmty:question label Apr 13, 2018
@HoraceKeung
Copy link

HoraceKeung commented Apr 13, 2018

Normally I would create a util.js file in /assets/js, it will look like:

const util = {
    myFunction1 () {
        // Do something
    },
   myFunction2 () {
        // Do something
    }
}

export default util

then import it in any vue file that need these functions, like:

<script>
import util from '~/assets/js/util'
export default {
    methods: {
        myFunction3 () {
            util.myFunction1()
        }
    }
}
</script>

if you don't want to import it manually in every vue file, then use a global mixin, https://vuejs.org/v2/guide/mixins.html#Global-Mixin, like this:
1, create a js file in the plugins folder (I normally just name it main.js)
2, then in main.js:

import Vue from 'vue'
import util from '~/assets/js/util'
Vue.mixin({
	methods: {
	    myFunction3 () {
            util.myFunction1()
        }
	}
})

3, add main.js in nuxt.config.js:

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

@Myolisi
Copy link
Author

Myolisi commented Apr 13, 2018

Thanks!! @HoraceKeung very helpful

@sudhir600
Copy link

sudhir600 commented Apr 30, 2018

Recently I did the same thing. I created a lib folder and placed all common type of function is separate file based of category (common.js, loginHelper.js etc) and just imported in a component or in mixins and then called it as you said.

However, I am not sure, if we can do this then my mixins come into existence.
what is the uses difference between normal js file and mixins way?

in mixing I found a strange issue but not in lib file..
I am still not able to understand the reason.

https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c7002

@lock
Copy link

lock bot commented Nov 2, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 2, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants