gpt4 book ai didi

angular - 使用angular ngrx store效果按id查询数据

转载 作者:行者123 更新时间:2023-12-03 23:14:35 24 4
gpt4 key购买 nike

我正在使用 ngrx 6 和 angular 6,我的代码如下所示:

  • 效果.ts
    import * as fromProjectMemberActions from '../project-member/project-member.actions';
    import * as fromProjectMemberGraphQL from '../../members/members.graphql';

    @Effect({dispatch: false})
    loadProjectMember$ = this.actions$.pipe(
    ofType(fromProjectMemberActions.ProjectMemberActionTypes.GetProjectMembers),
    tap(
    action => {
    this.apollo.watchQuery({
    query: fromProjectMemberGraphQL.GET_ALL_PROJECT_MEMBERS,
    variables: {
    projectID : // Project ID is needed
    }
    }).valueChanges.subscribe((response: any) => {
    console.log(response)
    this.store.dispatch(
    new fromProjectMemberActions.UpsertProjectMembers({
    projectMembers: response.data.findAllProjectMember
    })
    )
    this.store.dispatch(
    new fromProjectMemberActions.DoneLoadingProjectMembers()
    )
    })
    }
    )
    );
  • 项目-member.actions.ts
    export class GetProjectMembers implements Action {
    readonly type = ProjectMemberActionTypes.GetProjectMembers;
    }
    export class UpsertProjectMembers implements Action {
    readonly type = ProjectMemberActionTypes.UpsertProjectMembers;
    constructor(public payload: { projectMembers: ProjectMember[] }) {}
    }
  • members.graphql.ts
    import gql from 'graphql-tag';

    export const GET_ALL_PROJECT_MEMBERS = gql`
    query getAllProjectMembers($projectID: Long!) {
    findAllProjectMembers(projectId: $projectID) {
    id
    memberId
    memberType
    }
    }
    `;
  • 给定的 graphql API 在 graphql 操场上如下所示
    query{
    findAllProjectMembers(projectId: projectID) {
    id
    memberId
    memberType
    }
    }

    如果您将上面的 projectID 替换为数字,例如:2,它将成功查询数据
  • 将在编号 1 上使用的项目 ID 通过以下 URL 传递的 URL
    http://localhost/project-manager/view-project/2/members

  • 我需要帮助来提取上面的项目 ID 并在效果文件中使用它来查询成员列表

    最佳答案

    我会说。

    //更新 project-member.actions.ts

    export class GetProjectMembers implements Action {
    readonly type = ProjectMemberActionTypes.GetProjectMembers;
    constructor(public payload: { projectId: number }) {}
    }

    //并更新效果
    @Effect({dispatch: false})
    loadProjectMember$ = this.actions$.pipe(
    ofType(fromProjectMemberActions.ProjectMemberActionTypes.GetProjectMembers),
    map((action: any) => action.payload),
    tap(
    payload => {
    this.apollo.watchQuery({
    query: fromProjectMemberGraphQL.GET_ALL_PROJECT_MEMBERS,
    variables: {
    projectID : payload.projectId
    }
    }).valueChanges.subscribe((response: any) => {
    console.log(response)
    this.store.dispatch(
    new fromProjectMemberActions.UpsertProjectMembers({
    projectMembers: response.data.findAllProjectMember
    })
    )
    this.store.dispatch(
    new fromProjectMemberActions.DoneLoadingProjectMembers()
    )
    })
    }
    )
    );

    //可能在组件中或您调度操作的位置 Pass projectId
    subscribe to router {
    this.store.dispatch(new GetProjectMembers({ projectId })
    }

    关于angular - 使用angular ngrx store效果按id查询数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53532919/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com