新的生命周期函数, 介于beforeCreate和created之间一个声明周期函数, 有两种调用方法

    1. <template>
    2. <div>
    3. <ol>
    4. <li v-for="(item,index) in list" :key="index">{{index+1}}. {{item.name}}</li>
    5. </ol>
    6. </div>
    7. </template>
    8. <script>
    9. import { defineComponent, onMounted, ref } from "vue";
    10. import axios from 'axios';
    11. export default defineComponent({
    12. name: "App",
    13. setup() {
    14. // 定义一个响应式的变量list
    15. const list = ref([]);
    16. // 获取数据
    17. const getList = ()=> {
    18. axios.get('http://huruqing.cn:3000/api/film/getList').then(res=> {
    19. // 修改list,页面重新渲染
    20. list.value = res.data.films;
    21. })
    22. }
    23. // 在声明周期函数里自动调用getList去获取数据
    24. onMounted(getList);
    25. return {
    26. list,
    27. getList
    28. };
    29. },
    30. });
    31. </script>