Is there a solution to resolving the type error that I am unable to identify?

I am attempting to implement a custom cursor feature in Vue 3, but unfortunately my code is not functioning as expected. Below you can find the code snippet I have been working on:

    <template>
  <div id="cursor" :style="cursorPoint"></div>
</template>

<style>
  #cursor {
    position: fixed;
    width: 20px;
    height: 20px;
    border-radius: 100%;
    background-color: white;
    top: 0;
    left: 0;
    z-index: 10000;
  }
</style>

<script>
  export default {
    data() {
      return {
        x: 0,
        y: 0,
      }
    },
    methods: {
      moveCursor(e) {
        this.x = e.clientX - 15;
        this.y = e.clientY - 15;
      }
    },
    computed: {
      transformStyle: `transform: translate(${this.x}px,${this.y}px)`
    },
    mounted() {
      document.addEventListener("mousemove", this.moveCursor);
    }
  }
</script>

An error message that says the following appears in the console:

Uncaught TypeError: Cannot read properties of undefined (reading 'x')

Answer №1

Ensure that your calculation includes the cursorPoint variable:

    ...
    calculated: {
      cursorPoint() {
        return `position: absolute; transform: translate(${this.x}px,${this.y}px)`
      }
    },
    ...

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

Populating Hidden Form Fields with Dynamic Identifiers

I'm facing an issue with a form where I need to input a value into a hidden field upon clicking a button. Despite using unique IDs for all elements, the data is not being submitted in the POST request. The IDs are unique because there are multiple f ...

Select the input based on the name and value provided from a radio button using jQuery

I want to show a div element when the user chooses option 4 from a radio button. $(document).ready(function () { $("#GenderInAnotherWay").hide(); $("input[name='Gender'][value=4]").prop("checked", true); $("#GenderInAnotherWay").tog ...

Using optional chaining on the left side in JavaScript is a convenient feature

Can the optional chaining operator be used on the left side of an assignment (=) in JavaScript? const building = {} building?.floor?.apartment?.number = 3; // Is this functionality supported? ...

"Transforming a query into a JSON array - a step-by-step

My query generates the following output: { key:1,label:"R. Bulan"} { key:2,label:"R. Bintang"} { key:3,label:"R. Akasia"} { key:4,label:"R. Guest Room"} This is my SQL query: select '{ '||'key:'||IDMEETINGROOM||''||',l ...

comparison of declarative loop and imperative loop

In my journey to transition from an imperative programming style to a declarative one, I've encountered a challenge related to performance when dealing with loops. Specifically, I have a set of original DATA that I need to manipulate in order to achie ...

retrieveValue() for SelectionDropdown

I have a simple task - I just need to retrieve the name of the Company and store it in the database. Initially, I was able to achieve this using plain text and the code snippet below: sport: this.refs.company.getValue(), which worked perfectly. However, ...

Vue: JSON input ended abruptly while parsing near "...version":"0.5.0","dev"

Currently, I am diving into learning Vue JS with the intention of developing applications using it. To begin with, I went ahead and installed Vue by running the command: npm install vue-cli -g However, a close friend brought to my attention that this met ...

What steps do you take to establish a relay connection for pagination in an ORM framework?

After thorough research of Relay's documentation, I have yet to find a clear explanation on how to establish a Relay connection with an ORM. The examples provided mainly utilize the connectionFromArray method, which works well for data stored in memor ...

The authentication middleware is being executed for all routes within my app.js file, despite my intention to only apply it to a single route

I have developed a requireAuth middleware and integrated it into my app.js. In app.js, I have also imported all the routes from the routes folder. Each route.js file contains multiple chained routes. When I include the auth middleware in one of those files ...

What is the best way to toggle the visibility of my menu using JavaScript?

I recently implemented a script to modify a CSS property in my nav bar as I scroll down, triggering the change after reaching 100px. $(window).scroll(function() { var scroll = $(window).scrollTop(); //console.log(scroll); if ...

Retrieving session data from a different tab and website

The task at hand involves managing a PHP website (mysite.com) and an ASP.NET website (shop.mysite.com). The client's request is to implement a single sign-on solution for both sites. My approach is to develop a function on the ASP.NET site that can pr ...

Retrieve the inner content of parentheses within a string, utilizing recursion for nested parentheses

I am currently working on a function that will extract words enclosed in parentheses and store them in their own array, accounting for nested parentheses recursively. For example, when given the string "((a b) ugh (one two)) pi", I would like it to be tra ...

How can you retrieve the original file name and line number in exceptions that are generated in Angular controllers?

When an error occurs in my Angular controller, a stack trace is generated that typically looks like the following: TypeError: undefined is not a function at new <anonymous> (…/dist/script.js:854:5) at invoke (…/dist/base-script.js:13441: ...

Ensuring secure authentication in your Vue.js application using Vue router and Django

When it comes to checking if a user is authenticated on protected routes in vue-router, I have a setup using Django rest framework that sets sessionid upon login. While some users opt to use vuex or local storage to store session information, there's ...

What is the best way to manipulate arrays using React hooks?

Struggling with updating arrays using hooks for state management has been quite a challenge for me. I've experimented with various solutions, but the useReducer method paired with dispatch on onClick handlers seems to be the most effective for perform ...

There seems to be an issue with the VueJs + ElementUi Change method as it is

Just starting out with Vue and Element UI. I'm attempting to create a custom component using the ElementUI autocomplete/select feature. The problem I am facing is that the @change method does not contain a event.target.value value. When I try to acc ...

Adjusting ES2015 Map to accommodate the expected functionality

Exploring the capabilities of ES2015 Maps has been quite exciting, as I'm starting to see its potential. However, I've encountered a use case that has me stumped on whether Maps can handle it. Let's take a look at my class: class A { ...

Building better interfaces with Next.js, Styleguidist, and Fela for React applications

Has anyone successfully set up next.js with Fela and Styleguidist? I'm having trouble linking the Next.js webpack configuration to Styleguidist as mentioned in this article: I've been using this example app: https://github.com/zeit/next.js/tree ...

What is the best way to insert a newline in a shell_exec command in PHP

I need assistance with executing a node.js file using PHP. My goal is to achieve the following in PHP: C:proj> node main.js text="This is some text. >> some more text in next line" This is my PHP script: shell_exec('node C:\pr ...

Learn how to display separate paragraphs upon clicking a specific item

New to coding and eager to learn, I have recently started exploring HTML, CSS, and basic JavaScript. In my journey to enhance my skills, I am working on building a website for practice. One particular page of the site showcases various articles, each acc ...