发送请求,获得猫眼电影的数据
    https://m.maoyan.com/ajax/movieOnInfoList?token=&optimus_uuid=F04A6B70984011EB89DA9FC9709563EEE1FC4395B1554473B6696781D0EF2CDC&optimus_risk_level=71&optimus_code=10

    vue.config.js
    解决跨域问题

    1. module.exports = {
    2. devServer: {
    3. proxy: {
    4. "/ajax": {
    5. target: "https://m.maoyan.com",
    6. changeOrigin: true,
    7. },
    8. },
    9. },
    10. };

    scr/api/index.ts 数据请求写在该文件 统一一处书写数据请求,方便后期的测试和维护
    发送数据请求

    1. import axios from "axios";
    2. export const getMovieReq = () => {
    3. return axios({
    4. url: "/ajax/movieOnInfoList",
    5. params: {
    6. token: "",
    7. optimus_uuid:
    8. "F04A6B70984011EB89DA9FC9709563EEE1FC4395B1554473B6696781D0EF2CDC",
    9. optimus_risk_level: 71,
    10. optimus_code: 10,
    11. },
    12. });
    13. };

    src/store/index.ts vuex

    1. import { createStore } from "vuex";
    2. import * as api from "../api";
    3. const store = createStore({
    4. modules: {
    5. moviesStore: {
    6. namespaced: true,
    7. state: {
    8. movise: [],
    9. },
    10. actions: {
    11. async getMovise(store) {
    12. // todo 1、发送数据请求
    13. const res = await api.getMovieReq(); // 发送请求
    14. // console.log(res.data.movieList);
    15. // todo 2、创建action
    16. const action = {
    17. type:'GETMOVISE',
    18. payload:res.data.movieList
    19. }
    20. // todo 3、发送action
    21. store.commit(action)
    22. },
    23. },
    24. mutations: {
    25. GETMOVISE(state,action){
    26. state.movise=action.payload;
    27. }
    28. },
    29. },
    30. },
    31. });
    32. export default store;
    1. <template>
    2. <div>
    3. <ul>
    4. <li v-for="item in moviseList" :key="item.id">
    5. <h3>{{ item.nm }}</h3>
    6. <img :src="item.img.replace('w.h', '128.180')" alt="" />
    7. </li>
    8. </ul>
    9. </div>
    10. </template>
    11. <script lang="ts">
    12. import { computed, defineComponent, onMounted } from "vue";
    13. import { useStore } from "vuex";
    14. export default defineComponent({
    15. setup() {
    16. const store = useStore();
    17. const moviseList = computed(() => store.state.moviesStore.movise);
    18. onMounted(() => {
    19. store.dispatch({
    20. type: "moviesStore/getMovise",
    21. });
    22. });
    23. return {
    24. moviseList,
    25. };
    26. },
    27. });
    28. </script>