48d-数据标签

Huxzhi小于 1 分钟react18

48d-数据标签

[type: 'student'] 只要符合任意标签,就全部失效,范围太大了,设置id,更加精准

tagTypes: ['student'], 用来指定Api中的标签类型

providesTags: (result, error, id) => [{type: 'student', id}] 设置 添加标签

invalidatesTags: [{type: 'student', id: 'LIST'}] 设置 指定标签失效

const studentApi = createApi({
    reducerPath: 'studentApi', 
    baseQuery: fetchBaseQuery({
        baseUrl: "http://localhost:1337/api/"
    }),
    tagTypes: ['student'], // 用来指定Api中的标签类型
    endpoints(build) {
        return {
            getStudents: build.query({
                query() {
                    return 'students';
                },
                },
                providesTags: [{type: 'student', id: 'LIST'}]
            }),
            getStudentById: build.query({
                query(id) {
                    return `students/${id}`;
                },
                providesTags: (result, error, id) => [{type: 'student', id}]
            }),
            addStudent: build.mutation({
                query(stu) {
                    return {
                        url: 'students',
                        method: 'post',
                        body: {data: stu}
                    };
                },
                //设置失效标签
                invalidatesTags: [{type: 'student', id: 'LIST'}]
            }),
            updateStudent: build.mutation({
                query(stu) {
                    return {
                        url: `students/${stu.id}`,
                        method: 'put',
                        body: {data: stu.attributes}
                    };
                },
                invalidatesTags: ((result, error, stu) =>
                    [{type: 'student', id: stu.id}, {type: 'student', id: 'LIST'}])
            }),
        };
    }
});