3

I'm using an if statement to set page as follows:

 loadMaintenances (query = {}, status) {
             if(status === 'changed'){
                let page = 1
             }
             else{
                let page = this.page
             }

             console.log(page);
}

However, with the above code I'm getting the error;

[Vue warn]: Error in mounted hook: "ReferenceError: page is not defined"

But when I remove it outside of the if statement as follows:

   loadMaintenances (query = {}, status) {
              let page = this.page
             console.log(page);
}

It works. Is something wrong with my if statement?

1 Answer 1

5

This is indeed a kings territory problem, each data identifier has its own scope rules and in general 'let' has block scope so the variable that you define using 'let' inside a block is local only inside the block therefore when you try to print it outside it shows as undefined.

A simple solution for this would be declaring the variable page outside the if-block

loadMaintenances (query = {}, status) {
             let page;
             if(status === 'changed'){
                page = 1
             }
             else{
                page = this.page
             }

             console.log(page);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.