vue03.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板</title>
  6. <!-- 引入文件 -->
  7. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h3>显示图片</h3>
  12. <button @click="leftButton" >《《《</button>
  13. <!-- v-bind绑定 -->
  14. <!-- : 简写方式 -->
  15. <!--<img v-bind:src="imgsrc" width="200px" height="200px" >-->
  16. <img :src="imgsrc" width="200px" height="200px" >
  17. <button @click="rightButton" >》》》</button>
  18. </div> <!-- 容器 -->
  19. </body>
  20. <script>
  21. const { createApp, ref } = Vue;
  22. createApp({
  23. setup() {
  24. let imgsrc = ref("imgs/1.jpg")
  25. let index = 0
  26. //声明数组
  27. let imgs = [
  28. "imgs/1.jpg",
  29. "imgs/2.jpg",
  30. "imgs/3.jpg",
  31. ]
  32. function leftButton() {
  33. index--;
  34. //取绝对值
  35. imgsrc.value = imgs[Math.abs(index)%3];
  36. }
  37. function rightButton() {
  38. index++;
  39. imgsrc.value = imgs[Math.abs(index)%3];
  40. }
  41. return {
  42. rightButton, leftButton, imgsrc
  43. };
  44. }
  45. }).mount('#app'); //createApp创建对象 挂在到app
  46. </script>
  47. </html>