0π
β
You donβt want to create a new function in methods
, just add your code (but make sure mounted is async):
<script >
export default {
data() {
return {
projects: [
]
}
},
async mounted() {
const data = await fetch("https://jsonplaceholder.typicode.com/posts");
const newData = await data.json();
console.log(newData)
this.projects = newData;
}
}
</script>
You could do this:
async mounted() {
this.projects = await fetch("https://jsonplaceholder.typicode.com/posts").then(raw => raw.json());
}
π€tauzN
3π
The error is that you are using this
within a function ()
notation. In order to make it work as you expect, you should use an arrow function.
const test = async () => {
//...
}
π€Lykos94
0π
First, test() should be a method. Second, place the closing bracket in projects on the same line.
π€Eze Kohon
Source:stackexchange.com