Skip to content
Advertisement

Why does data in my JavaScript class is undefined when calling it from a component?

I have in an external JavasScript (sun.js) file a class like this :

JavaScript

So I’m importing this class in one of my component

JavaScript

And then I call my function testIfShining() in my mounted lifecycle :

JavaScript

When I look at my console, I have the message log

JavaScript

The function is working but I have an undefined value for the data this.text

How can I reuse the value inside my constructor? I want to my data works like attribute so I can reuse it in every function in my class.

Advertisement

Answer

If you write/copy your code well here you have to escape the single quote. Like

JavaScript

in your code

JavaScript

edit: Reply to fast. And also use like this :

Could you try this

JavaScript

example: https://jsfiddle.net/y4675twv/2/

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement