First steps with Alpine. In my example the reactivity does not work #2899
Answered
by
SimoTod
josejachuf
asked this question in
1. Help
-
I'm trying to learn Alpine. Given the following example:
Why it is not reactive?. When I press the button I hope to obtain the todos list and the quantity. The call with axios runs fine and I get the todos in the function, but I do not see them reflected in the HTML. I imagine I'm doing something wrong Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
SimoTod
May 3, 2022
Replies: 1 comment 1 reply
-
You need to use arrow functions. axios.get('https://jsonplaceholder.typicode.com/todos/')
.then((response) => {
this.todos = response.data;
this.count = this.todos.length;
console.log('Count', this.count);
}).catch((error) => {
console.log(error);
}).then(function () {
// always executed
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
josejachuf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to use arrow functions.
this
gets redefined in standard functions so it won't point to your alpine component (It's not an Alpine thing, it's how javascript works).