2

New to Vue, trying to figure out something. I'm using this "Data":

data() {
        return {
            currentPage: 0,
            nextPage: '',
            previousPage: '',
            left: '',
            opacity: '',
            scale: '',
        }
    },

Trying to use the data variables inside a method looks like this:

methods: {
        isStageOneDone: function () {
            var animating;
            if(animating) return false;
            animating = true;
            this.currentPage;
            console.log("CurrentPage =>", currentPage);
   }
}

But I keep getting this error:

Uncaught ReferenceError: currentPage is not defined

What am I missing? I looked into the Vue docs and it seems ok I think

Edit: Is it possible that because of the return() the error occurs?

3
  • Parentheses must be wrapped around objects which are returned or it will not be interpreted correctly. (Edit: This is because return { will be interpreted as return {; due to what's known as Automatic Semicolon Insertion. For more info, see stackoverflow.com/questions/8528557/…) Commented Mar 19, 2021 at 12:05
  • I don't understand what u mean,I'm sorry Commented Mar 19, 2021 at 12:12
  • The code inside your function doesn't make much sense. In order for anyone to be able to help you, we'd need to at least understand what it is you are trying to achieve. As a sidenote, your code does try to use a variable (currentPage) which has not yet been defined in the scope of your function (because this.currentPage is not currentPage). However, even if you did define it prior to using it, you'd only remove the error, it wouldn't do anything. So, what are you trying to do with that code? Commented Mar 19, 2021 at 12:20

2 Answers 2

1

You have to add "this" key for access to defined inside data attributes. for example:

data(){
return{
  variable:'example'
}
},
methods:{
  
  exampleFunc(){
  
  
  return this.variable;
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, i'm wondering if u can help me with something else?
0

Hi you should define your methods like below

methods: {
 isStageOneDone(path, data) {
        var animating;
        if(animating) return false;
        animating = true;
        this.currentPage;
        console.log("CurrentPage =>", this.currentPage);
 }
}

lets try it out i think it will work for you

3 Comments

yes because you forget to mention "this" before currentPage
as so I, I updated the answer please check it out
Oh, bad habits..i will learn for the feature