美文网首页
一开始就执行

一开始就执行

作者: 糖醋里脊120625 | 来源:发表于2022-06-27 10:32 被阅读0次

    app.vue

    <template>
      <router-view #="{ Component }">
        <component :is="Component" />
      </router-view>
    </template>
    
    <script lang="ts">
    import { useStore } from 'vuex'
    import { defineComponent } from 'vue'
    export default defineComponent({
      name: 'App',
      setup() {
        const store = useStore()
        const getLabelList = () => store.dispatch('label/getLabelList')
    //执行这个方法就会调用 actions 中的getLabelList方法
        getLabelList()
      },
    })
    </script>
    
    

    \store\index.ts

    import { createStore } from "vuex";
    import label from "./modules/label";
    
    export default createStore({
      state: {},
      mutations: {},
      actions: {},
      modules: {
        label,
      },
    });
    
    

    store\modules\label.ts

    import { apiGetLabelList } from "@/api/label";
    import { labelModel } from "@/models/index";
    
    type State = {
      labelList: Array<labelModel>;
    };
    export default {
      namespaced: true,
      state: {
        labelList: [ {
          _id: "1",
          label: "查询记录_蘑菇",
          bgColor: "string",
          createTime: "string",
        }],
      },
      mutations: {
        setLabelList(state: State, data: Array<labelModel>) {
          state.labelList = data;
        },
      },
      actions: {
        getLabelList(context: { commit: (arg0: string, arg1: any) => void }) {
          let params = {
            pageindex: 1,
            pagesize: 50,
          };
          return apiGetLabelList(params)
            .then((res) => {
              context.commit("setLabelList", res?.data?.list);
            })
            .catch((err) => {
              console.log(err);
            })
            .finally(()=>{
              alert(2)
            })
            ;
        },
      },
      getters: {
        labelList(state: State) {
          return state.labelList;
        },
      },
    };
    
    

    import { labelModel } from "@/models/index.ts";

    interface articleModel {
      _id: string;
      title: string;
      releaseTime: string;
      pv: number;
      auth?: string;
      type: string[];
      desc?: string;
      fileCoverImgUrl: string;
      html: string;
      markdown?: string;
      level?: number;
      github?: string;
      source?: number;
      isVisible: boolean;
      likes: number;
      comments?: number;
    }
    
    interface labelModel {
      _id: string;
      label: string;
      bgColor: string;
      createTime?: string;
    }
    
    type replyItem = {
      _id: string;
      replyHeaderColor?: string;
      replyContent?: string;
      replyUser: string;
      byReplyUser: string;
      replyTime: string;
    };
    interface commentModel {
      _id: string;
      content: string;
      headerColor: string;
      nickname: string;
      createTime: string;
      likes: number;
      comments: number;
      replyList?: Array<replyItem>;
    }
    
    export type { articleModel, labelModel, commentModel, replyItem };
    

    相关文章

      网友评论

          本文标题:一开始就执行

          本文链接:https://www.haomeiwen.com/subject/kmmnvrtx.html