0

假设我有一个方法

const actions = {
  async fetchByQuery({
    commit
  }, title) {
    const response = await..........code goes here
  }
}

我想在这样的方法中使用我自己的另一个函数:

        const actions = {
          async fetchByQuery({
            commit
          }, title) {
            const response = await..........code goes here
            
            this.helperfunction();
          }
          
          helperfunction(){
             ......code goes here
          }
        }

我该如何使用辅助功能?

我尝试了上述方法并得到错误this.helperfunction is not a function

4

1 回答 1

0

您始终可以在商店之外导入功能,它们不必是其中的一部分。

// Either

import HelperFunction from "./helperfunction.js

// OR:

const HelperFunction = () => {
  console.log("Hello world!");
}

const actions = {
  async fetchByQuery({
    commit
  }, title) {
    const response = await..........code goes here
    
    // Use the helper function without `this`
    let formattedResponse = HelperFunction(response);
    
    commit('saveState', formattedResponse); 
  }
  
}

值得注意的是,他们无法直接访问修改商店,但这可能正是您所需要的。如果不知道您问题的上下文,很难说。

于 2020-07-14T10:13:04.353 回答