FetchData.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <template>
  2. <h1 id="tableLabel">Weather forecast</h1>
  3. <p>This component demonstrates fetching data from the server.</p>
  4. <p v-if="!forecasts"><em>Loading...</em></p>
  5. <table class='table table-striped' aria-labelledby="tableLabel" v-if="forecasts">
  6. <thead>
  7. <tr>
  8. <th>Date</th>
  9. <th>Temp. (C)</th>
  10. <th>Temp. (F)</th>
  11. <th>Summary</th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15. <tr v-for="forecast of forecasts" v-bind:key="forecast">
  16. <td>{{ forecast.date }}</td>
  17. <td>{{ forecast.temperatureC }}</td>
  18. <td>{{ forecast.temperatureF }}</td>
  19. <td>{{ forecast.summary }}</td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. </template>
  24. <script>
  25. import axios from 'axios'
  26. export default {
  27. name: "FetchData",
  28. data() {
  29. return {
  30. forecasts: []
  31. }
  32. },
  33. methods: {
  34. getWeatherForecasts() {
  35. axios.get('/weatherforecast')
  36. .then((response) => {
  37. this.forecasts = response.data;
  38. })
  39. .catch(function (error) {
  40. alert(error);
  41. });
  42. }
  43. },
  44. mounted() {
  45. this.getWeatherForecasts();
  46. }
  47. }
  48. </script>