如何从另一个函数访问Javascript中的嵌套函数?

问题描述 投票:1回答:2

如果有的话

import axios from "axios";

function model() {
  function getAll() {
    return axios
      .get("http://localhost:3000/teams")
      .then(response => response.data);
  }
}

export default model;

如何从另一个组件访问getAll()方法?

我尝试导入model,然后将其引用给getAll-model.getAll(),但它抱怨该方法未定义。

我尝试引用Calling a Function defined inside another function in Javascript,但找不到解决方案。

这甚至是正确的方法吗?

javascript function
2个回答
3
投票

除了getAll内部,您无法从任何地方访问model。也许您打算创建一个对象?

var model = {
    getAll: function () { ... }
}

model.getAll();

0
投票

您总是可以实例化该功能

function model() {
  this.getAll = () => {
    console.log("hello world");
  };
}

const myFunc = new model();

myFunc.getAll() // console.log('hello world')
© www.soinside.com 2019 - 2024. All rights reserved.