【问题标题】:Object is possibly 'undefined' in Vuex mutation with TypeScript在使用 TypeScript 的 Vuex 突变中,对象可能是“未定义的”
【发布时间】:2020-06-27 22:54:09
【问题描述】:

我正在使用 Vuex 和 TypeScript 学习 Vue.js,在我正在构建的应用程序中,我在 Vuex 商店中遇到了错误“对象可能是'未定义'”。

错误发生在这一行的“newCard”突变中:

state.board.lists.find(list => list.id === idList).cards.unshift(card)

完整的店铺代码:

const state: BoardState = {
  board: {
    id: "",
    name: "",
    idProject: "",
    closed: false,
    lists: []
  }
};

const getters: GetterTree<BoardState, {}> = {
  getBoard: state => state.board
};

const mutations: MutationTree<BoardState> = {
  setBoard: (state, board) => (state.board = board),
  newList: (state, list) => state.board.lists.unshift(list),
  newCard: (state, { card, idList }) =>
    state.board.lists.find(list => list.id === idList).cards.unshift(card)
};

const actions: ActionTree<BoardState, {}> = {
  async fetchBoard({ commit }) {
    const response = await axios.post("https://app.fakejson.com/q", json);
    commit("setBoard", response.data);
  },
  async addList({ commit }, list) {
    const response = await axios.post("https://app.fakejson.com/q", {
      token: "oplF0L7vj1Ial4cRqtx9DA",
      data: list
    });
    commit("newList", response.data);
  },
  async addCard({ commit }, { card, idList }) {
    const response = await axios.post("https://app.fakejson.com/q", {
      token: "oplF0L7vj1Ial4cRqtx9DA",
      data: card
    });
    commit("newCard", response.data, idList);
  }
};

TypeScript 类型:

// Store
export interface BoardState {
  board: Board;
}

// Models
export interface Board {
  id: String;
  name: String;
  idProject: String;
  closed: Boolean;
  lists: List[];
}

export interface List {
  id: String;
  name: String;
  idBoard: String;
  closed: Boolean;
  cards: Card[];
}

export interface Card {
  id: String;
  name: String;
  idList: String;
  closed: Boolean;
}

棋盘状态的响应数据是这样的:

 {
   "id":"1",
   "name":"Tasks",
   "idProject":"1",
   "closed":false,
   "lists":[
      {
         "id":"1",
         "name":"To Do",
         "idBoard":"1",
         "closed":false,
         "cards":[
            {
               "id":"1",
               "idList":"1",
               "name":"Card 1",
               "closed":false
            },
            {
               "id":"2",
               "idList":"1",
               "name":"Card 2",
               "closed":false
            }
         ]
      }
   ]
}

【问题讨论】:

    标签: javascript typescript vue.js vuex


    【解决方案1】:

    state.board.lists.find(list =&gt; list.id === idList).cards.unshift(card)

    可能找不到具体的列表。所以你将无法从中挑选卡片。

    const list = state.board.lists.find(list => list.id === idList)
    
    if (!list) {
        // do something
        return
    }
    
    // list found so return
    return list.cards.unshift(card)
    

    【讨论】:

    • 之前的错误消失了,但 idList 参数在突变中未定义,我将它作为 AddCard 组件中的道具传递,它显示在控制台中,但不在突变中。跨度>
    猜你喜欢
    • 2021-03-31
    • 2018-08-17
    • 1970-01-01
    • 2020-05-11
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多