Home > other >  Convert meta content to json?
Convert meta content to json?

Time:11-17

I am using laravel's sanctum auth and I want to use user info in vue's global components, but it's being stored as a string and I can't access the data easily...

Adding user to header:

<meta name="user" content="{{ Auth::user('sanctum') }}">

Storing the content to a prototype:

Vue.prototype.$user = document.querySelector("meta[name='user']").getAttribute('content');

But the problem is that it's just a string: data example

so getting the id or any other data like this:

console.log(this.$user.id);

doesn't work

Any way I can fix this?

CodePudding user response:

You have to parse the string to a JSON Object with JSON.parse() change this line: Vue.prototype.$user = document.querySelector("meta[name='user']").getAttribute('content')

to:

Vue.prototype.$user = JSON.parse(document.querySelector("meta[name='user']").getAttribute('content'))

  • Related