vue.js - Access to vue properties -


in authority system store secret token in vue fields , localstorage in created event:

const app = new vue({     router: router,     data: {         token: '',         user: null,     },     created: function () {         var token = localstorage.getitem('token');         if (token) {             this.token = token;         } ... 

then i'm trying fetch token in beforerouteenter method of compomnent:

beforerouteenter: function (to, from, next) {     var id = to.params.id;     console.log(router.app.$root.token); }, 

but field empty, defined later. how correct structure of application send api requests token?

you can pass callback next, have access instance of vue component being routed to.

from documentation:

the callback called when navigation confirmed, , component instance passed callback argument

in case, this:

beforerouteenter: function (to, from, next) {   var id = to.params.id;   next((vm) => {     console.log(vm.$root.token);   }) }, 

now, correct structure application send api requests token? that's hard without more context , broad. i'd think store token in whatever library you're using api requests , that'd better referencing each time route changes.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -