Nuxt.js: How to get access to vuex/vue-router in plugins js-files

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

Have file for custom instance of axios in ~/plugins/axios.js
How to get access to store or router inside this file?
For example for correct handling 401-error and redirecting user to login-page with router.push of commiting mutation to store, which redirect user

P.S.: Nuxt 1.0.0-RC11

This question is available on Nuxt.js community (#c1726)

Most helpful comment

The plugin can be export a function that takes the context as an argument. See the plugins section.

// ~/plugins/axios.js
export default ({ store, redirect }) => {

}

All 6 comments

The plugin can be export a function that takes the context as an argument. See the plugins section.

// ~/plugins/axios.js
export default ({ store, redirect }) => {

}

I'm trying to do this as well - use the store in axios plugin. Tried the solution above but get the error:
Cannot read property 'store' of undefined

Any help?

@tobz-nz I got the same problem, Have you solved this?

I think so...

If your in an action method then this refers to the root store, so this.$router.push(URL_URL_PATH) should work.

Thank you for your reply @tobz-nz

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

uptownhr picture uptownhr  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments

vadimsg picture vadimsg  路  3Comments

mattdharmon picture mattdharmon  路  3Comments