I get data back from my API when some parameters are correct.
Now I need to check if I have some data inside of current_data or not and want to change the definition of my array as you can see below.
Actually what I have tried didn't work, so it would be a big pleasure if someone can help me out!
My error: if(current_data != null) gives me multiple errors that it's not a expected declaration.
mounted() {
get_Data() {
var url = "justexample/API"
fetch(url)
.then((response) => {
return response.json();
})
.then((data) => {
this.current_data = data;
})
data() {
return {
current_data: [],
//Here I want to check if current data == [] like this:
if(current_data != null) {
array: [{
id: this.id =1,
name: "",
}]
} else if(current_data == null {
array: []
}
}
}
CodePudding user response:
First of all when posting questions: what didn't work? Did you get any errors?
You can't put logic/ifs in an object returned by the data function!
You probably have to put your if into a computed:
data() {
....
},
computed: {
counter() {
return this.current_data?.length > 0 ? 1 : 0; // if the current_data array has 0 items return 0, otherwise 1
}
}
Then you can use the counter computed in the template:
<span>{{ counter }}</span>
CodePudding user response:
You can do like that:
data() {
return {
current_data: [],
//Here I want to check if current data == [] like this:
counter: null;
}
}
mounted() {
get_Data() {
var url = "justexample/API"
fetch(url)
.then((response) => {
return response.json();
})
.then((data) => {
this.current_data = data;
if(current_data == []){
this.counter = 0
}
} else if(current_data != [] {
this.counter = 1
}
})
