Reputation: 131
im relative new to Vue.js but now i stuck on a maybe stupid simple problem.
I have two files, one for the routes and one for a plugin.
auth.js
export default {
install: (Vue, options) => {
Vue.prototype.$auth = {
login: (email, password) => {
...
}
};
}
};
and the router.js
import Vue from 'vue'
import Router from 'vue-router'
import auth from '../auth'
Vue.use(auth);
Vue.use(Router);
const router = new Router({
...
routes: [
{
path: '/',
name: 'index',
component: function(resolve) {
require(['../../components/list/index.vue'], resolve)
}
},
{
path: '/admin',
name: 'adm',
component: function(resolve) {
require(['../../components/admin/event.vue'], resolve)
},
beforeEnter: guardRoute
}
]
});
function guardRoute (to, from, next) {
console.log('?');
}
export default router;
Now in the guardRoute function i want to call the plugin, but i don't know how. I tried something like console.log(auth) but there is only the install() function but not the $auth object. I also tried console.log(Vue) or console.log(router) but i'm not able to call/find the login()-function or the $auth object from the plugin in the console output. So what am i doing wrong? Can anybody help me with this?
Upvotes: 1
Views: 2937
Reputation: 55644
The auth plugin is adding the $auth
object to the Vue.prototype
. So, you need to first get a reference to the related Vue instance in the beforeEnter
navigation guard.
To do that, you'll need to pass a callback to the next
parameter. The Vue instance is the first parameter in the callback:
function guardRoute(to, from, next) {
next(vm => vm.$auth.login('email', 'password'));
}
Here's the documentation on the navigation guards.
Upvotes: 2