link1507 link1508 link1509 link1510 link1511 link1512 link1513 link1514 link1515 link1516 link1517 link1518 link1519 link1520 link1521 link1522 link1523 link1524 link1525 link1526 link1527 link1528 link1529 link1530 link1531 link1532 link1533 link1534 link1535 link1536 link1537 link1538 link1539 link1540 link1541 link1542 link1543 link1544 link1545 link1546 link1547 link1548 link1549 link1550 link1551 link1552 link1553 link1554 link1555 link1556 link1557 link1558 link1559 link1560 link1561 link1562 link1563 link1564 link1565 link1566 link1567 link1568 link1569 link1570 link1571 link1572 link1573 link1574 link1575 link1576 link1577 link1578 link1579 link1580 link1581 link1582 link1583 link1584 link1585 link1586 link1587 link1588 link1589 link1590 link1591 link1592 link1593 link1594 link1595 link1596 link1597 link1598 link1599 link1600 link1601 link1602 link1603 link1604 link1605 link1606 link1607 link1608 link1609 link1610 link1611 link1612 link1613 link1614 link1615 link1616 link1617 link1618 link1619 link1620 link1621 link1622 link1623 link1624 link1625 link1626 link1627 link1628 link1629 link1630 link1631 link1632 link1633 link1634 link1635 link1636 link1637 link1638 link1639 link1640 link1641 link1642 link1643

[Vue.js] Cannot get cookie in vue-router to work. Undefined this Subscribe to RSS

The problem is that I cannot get cookie in vue-router. When I try to do this: this.$cookie.get(‘token’); I get this undefined. Even though I can use this.$cookie in vue.js components. This is my code snippet:

Vue-Router:

This is a function to check if I get token from cookie (but it doesnt work):

function isLoggedIn (to, from, next) {
console.log(this.$cookie.get(‘token’));
}

And this is the route and I use that isLoggedIn in it:

{
path: ‘/‘,
name: ‘login’,
component: Login,
beforeEnter: isLoggedIn()
},

Maybe someone knows how to get that cookie ? It it setted up in vue.js component correctly just don’t know how to pass it to vue.js router.

EDIT
So by using Vue.cookie.get(‘token’); I get that next is not a function. This is the current function:

function isLoggedIn (to, from, next) {
if(Vue.cookie.get(‘token’)) {
next({name: ‘game’});
} else {
next();
}
}

Okay so when I added this function directly into route like this it worked:

{
path: ‘/‘,
name: ‘login’,
component: Login,
beforeEnter: (to, from, next) => {
if(Vue.cookie.get(‘token’)) {
next(‘/game’);
} else {
next;
}
}
},

Solution :

If you are using the vue-cookie plugin from npm then you will need to use this syntax outside of components:

function isLoggedIn (to, from, next) {
console.log(Vue.cookie.get(‘token’));
}

With regards to the next is not a function error, this is because you are invoking the function with no arguments, isLoggedIn(), rather than passing the function reference, isLoggedIn, which will be invoked by the router with the appropriate arguments.

Try this instead:

{
path: ‘/‘,
name: ‘login’,
component: Login,
beforeEnter: isLoggedIn // No parentheses here
},

I hope this helps.

Solution 2:

If you look at the typings, you’ll see that both to and from are Route’s. Here’s their interface declaration:

export interface Route {
path: string;
name?: string;
hash: string;
query: Dictionary<string | (string | null)[]>;
params: Dictionary<string>;
fullPath: string;
matched: RouteRecord[];
redirectedFrom?: string;
meta?: any;
}

If look at the Definition of BeforeEnter:

beforeEnter?: NavigationGuard;

If you look at the Definition of NavigationGuard:

export type NavigationGuard<V extends vue.js = Vue> = (
to: Route,
from: Route,
next: (to?: RawLocation | false | ((vm: V) => any) | void) => void
) => any

So as we can see, the beforeEnter returns a closure that exposes 3 methods to us: (to, from, next).

As we can see the typings of both to and from are just Route Interfaces and next is a function that we pass arguments into, we can safely determine that the scope of this is indeed, undefined.

So you can either define it in the meta of the Route declaration and access it with this.$meta.cookies, or you can import the cookie package directly and use it with cookies.get(‘token’) or you can augment the return type of the beforeEnter method:

beforeEnter: (to, from, next) => isLoggedIn(to, from, next, $cookies)

You still, cannot, use the in component guards as they do not expose this to you because they execute before the component has been created