Vue.js template: Use async/await

This commit is contained in:
Murat Girgin 2017-10-04 20:36:45 -07:00 committed by Steve Sanderson
parent 40603f1566
commit 5e752169d6
1 changed files with 9 additions and 6 deletions

View File

@ -12,11 +12,14 @@ interface WeatherForecast {
export default class FetchDataComponent extends Vue {
forecasts: WeatherForecast[] = [];
mounted() {
fetch('api/SampleData/WeatherForecasts')
.then(response => response.json() as Promise<WeatherForecast[]>)
.then(data => {
this.forecasts = data;
});
async mounted() {
try {
let response = await fetch('api/SampleData/WeatherForecasts');
let data = await response.json();
this.forecasts = data;
}
catch (err) {
console.log(err);
}
}
}