vue3.0请求接口的例子


<script>
import { getInTheaters } from "@/api/movie";
import {
  reactive,
  toRefs,
  onBeforeMount,
  onMounted,
  onBeforeUnmount,
  provide,
} from "vue";
import { useRouter } from "vue-router";
import { useStore } from 'vuex'
export default {
  components: {},
  setup() {
    const state = reactive({
      inTheaters: [],
    });

    // 编程式导航
    const router = useRouter();
    const store = useStore();

    onBeforeMount(() => {
      store.commit("setShowBack", false);
    });

    onBeforeUnmount(() => {
      store.commit("setShowBack", true);
    });

    const getInTheatersData = async () => {
      const res = await getInTheaters(
        "/gateway?cityId=310100&pageNum=1&pageSize=10&type=1&k=3535827"
      );
      state.inTheaters = res.data.data.films;
      console.log(res.data.data.films);

      onMounted(() => {
        getInTheatersData();
      });

      const goToList = (type) => {
        router.push(`/list/${type}`);
      };

      // 传递数据给子组件
      provide("title", "黑马电影");

      return { ...toRefs(state), goToList };
    };
  },
};
</script>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM