Skip to content

通过方法访问 ​

本节目标:理解“通过方法访问”的核心思路,并能把它用于实际开发或面试表达。 你也可以通过让 getter 返回一个函数,来实现给 getter 传参。在你对 store 里的数组进行查询时非常有用。

getters: {  // ...  getTodoById: (state) => (id) => {    return state.todos.find(todo => todo.id === id)  }}
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }

注意: getter 在通过方法访问时,每次都会去进行调用,而不会缓存结果。

本站总访问 — 次