I created a const inside this.api.getApi().subscribe(({tool,beuty}), how can I run it inside my if statement like this:
if (evt.index === 0) {aa}
I will create more lines like this and I want to run them with different if statements:
this.beu=beuty.slice(0,this.i+=15);
Is there a better solution to do that?
Code:
onScrollDown(evt:any ) {
setTimeout(()=>{
this.api.getApi().subscribe(({tool,beuty}) => {
const aa=this.beu=beuty.slice(0,this.i+=15);
})
if (evt.index === 0) {aa}
},1000);
}
You can't access a variable declared using const outside its block scope. Try the following and you will get an ReferenceError:
function foo() {
const bar = "bar";
}
console.log(bar);
So, you can't use aa outside of the callback passed to this.api.getApi().subscribe().
If you want to be able to use aa outside of the callback you can define aa using let. In code:
onScrollDown(evt:any) {
setTimeout(() => {
// Add this:
let aa;
this.api.getApi().subscribe(({tool,beuty}) => {
// Change this:
aa=this.beu=beuty.slice(0,this.i+=15);
})
if (evt.index === 0) {aa}
}, 1000);
}
I hope this helps. I can't really tell if it works because I don't have the whole script.