Unable to access Vue.js cookies: they are not defined

I integrated vue-cookies into my project successfully.

In the main.js file, I added the following code:

createApp(App)
    .use(store)
    .use(router, axios, VueCookies)

The script section in App.vue file looks like this:

<script>
import Navbar from '@/components/layout/Navbar'
import axios from 'axios'


export default {
  name: 'App',
  components: {
    Navbar
  },
  beforeCreate() {


    console.log(this.$cookies)

However, it seems that cookies are undefined, preventing me from setting or retrieving any cookie values.

Even after including

import VueCookies from 'vue-cookies'
, the issue persists.

Prior to this, I utilized localStorage to store login information and accessed it within the beforeCreate lifecycle hook. Now, I want to switch to using cookies for storing tokens across sessions due to enhanced security benefits.

Despite conducting some research, I could not find any information on initializing cookies or any additional steps needed to prevent this.$cookies from being undefined.

Answer №1

I haven't had the chance to explore this particular library yet, but upon checking out npm: https://www.npmjs.com/package/vue-cookies

To start using it, you will need to import the library and activate the plugin.

import VueCookies from 'vue-cookies';

Vue.use(VueCookies)

***Assuming that you have already installed the library beforehand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to transfer data from a JavaScript file to the Express server without relying on a form submission?

As a newcomer here, I apologize if I overlook any best practices. Currently, I am in the process of developing a sudoku game using express, JavaScript, HTML, and mongoDb. My current challenge involves setting up a statistics system. My goal is to send da ...

What could be causing these warnings to pop up when I am utilizing the useEffect hook in React.js?

Having some trouble with React hooks and JS. I keep getting warnings about missing dependencies in my code, even after reading the documentation. It's all a bit confusing to me. ./src/CustomerList.js Line 32:6: React Hook useEffect has a missing d ...

In Typescript, we can streamline this code by assigning a default value of `true` to `this.active` if `data.active

I am curious if there is a better way to write the statement mentioned in the title. Could it be improved with this.active = data.active || true? ...

Ensure that all content in the table rows remains visible, even in a table with unusually lengthy cells

My HTML table is structured like this: +-------------+-------------+----------+ | Single line | Single line | Very, | | | | very | | | | long | | | | text, | | ...

Exploring the scope of event listeners in jQuery's TimeCircles

Currently experimenting with the jquery timer known as "TimeCircles", which can be found here. I have successfully set up a minute / second timer for 30 minutes with an alert that triggers at 25 minutes. My goal is to implement an event when the timer hits ...

Is there a way to replicate ajaxStart and ajaxStop functions without using jQuery?

After reviewing the extensive jQuery code, I'm wondering if this task would be simple to accomplish. Any suggestions on how to approach it? I'm interested in using this not for a webpage, but for a C# application that needs to monitor ajax activ ...

verifying if checkbox is selected using a while loop in PHP

Help Needed: I am currently trying to loop through some code, but I'm struggling with checking checkboxes using PHP. Could someone please review my code and provide guidance on what needs to be added? Any assistance would be greatly appreciated. Thank ...

Using expressJS and MongoDB to create a secure login and registration system

I am interested in adding a login/register function to my expressJS API. Currently, I am only inserting the password and email into my database. I would like this function to first check if a user with this email is already in the database - if yes, then s ...

Tips for incorporating Javascript Object Literals into Python code

Using the Beautifulsoup module, I successfully extracted an HTML page and then proceeded to extract a Javascript script tag from that page. Within this script tag lies an object literal that I hope to manipulate. Here is what I am aiming for: <script&g ...

Utilizing javascript to reverse an array and seamlessly filling in the missing elements

Consider an array containing data with increasing percentage values and some missing entries. For instance: { "months": 11, "factor": 1.31, "upperMonths": 10.5, "lowerMonths": 11.49, "limit": 20, "percentage": 8 }, { "mont ...

How can I implement a progress bar that mimics an exchange platform behind data-table components? [Using Vue and Vuetify]

I am working on a cryptocurrency exchange book simulator and I am trying to implement a progress bar that will be displayed behind the values in a table. https://i.stack.imgur.com/9gVsY.png This is the template for my data-table: < ...

Bring in all SCSS styles from a single file and apply them to a React component

I am attempting to incorporate the entire SCSS file into a React component. I attempted to use the styleName props but was unsuccessful import React from 'react' import Calendar from 'calendar' import { calendarStyles } from './ ...

What is the best way to eliminate the default hover effect using the MenuItem Mui class?

My goal is to eliminate the default gray hover over animation when using the MUI menu item class. I have attempted several methods, but none have been successful so far. Here are a couple of examples: <MenuItem divider sx={{'&:hover':{bac ...

What is the reason for JSHint alerting me about utilizing 'this' in a callback function?

While working in my IDE (Cloud 9), I frequently receive warnings from JSHint alerting me to errors in my code. It is important to me to address these issues promptly, however, there is one warning that has stumped me: $("#component-manager tr:not(.edit-co ...

React Select only enabling single selection at a time

Currently, I am in the process of updating my react app to version 16.8 and also updating all associated libraries. One issue that has come up is with two drop-down selects from Material UI. Since the update, the multi-select option no longer allows multip ...

Challenges with creating an increment and decrement form component using Vue

I've been working on this for almost three days now. My goal is to create a reusable component in Vue that allows a passed value to be incremented and decremented with the click of a button, and then submitted in a form. Below is my code: Parent Com ...

Need some assistance in finding a way to input multiple values from multiple buttons into a single input field in JavaScript

Hello, I am looking for a little help with reading multiple values using multiple buttons such as 1, 2, and 3, and displaying the output in the input like '123' instead of just one number at a time. Concatenate numbers with every click. <inpu ...

Enumeration field with Conditional logic

I am currently developing a Content Management System (CMS) using strapi for a client, and I want to provide them with the ability to control the questions included in a questionnaire. Each question will be categorized under different sections in the quest ...

Mastering hot reloading in React when using Dotnet and DockerorAchieving seamless hot reloading in

I have been working on getting hot reloading to function properly with React, Docker, and Dotnet. However, my research has shown that only static rendering is compatible with Docker. As a result, I find myself having to run the command docker -t build {Na ...

Issue with Javascript functionality not persisting after page reload initiated with a href = '#'

I am facing an issue with a button on my webpage that is meant to redirect and reload the home page. However, after redirection to '#', my JavaScript seems to stop functioning correctly. Currently, my JavaScript code is enclosed within window.on ...