提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
856 B

  1. <template>
  2. <div class="person">
  3. <h2>当前求和为{{sum}}</h2>
  4. <button @click="add">点我sum+1</button>
  5. <br>
  6. <img v-for="(dog,index) in dogList" :src="dog" :key="index" >
  7. <button @click="getDog">再来一只狗</button>
  8. </div>
  9. </template>
  10. <script lang="ts" setup name="Person">
  11. import { ref,reactive } from 'vue';
  12. import axios from 'axios';
  13. //数据
  14. let sum = ref(0)
  15. let dogList = reactive([
  16. 'https://images.dog.ceo/breeds/pembroke/n02113023_4269.jpg'
  17. ])
  18. //方法
  19. function add(){
  20. sum.value+=1
  21. }
  22. async function getDog(){
  23. try{
  24. let result = await axios.get('https://dog.ceo/api/breeds/image/random')
  25. dogList.push(result.data.message)
  26. }catch(error){
  27. alert(error)
  28. }
  29. }
  30. </script>
  31. <style scoped>
  32. .person{
  33. width: 200px;
  34. height: 200px;
  35. background-color: palegoldenrod;
  36. }
  37. img{
  38. width: 100%;
  39. height: auto;
  40. }
  41. </style>