问题
In case I have
import axios from "axios";
function model() {
function getAll() {
return axios
.get("http://localhost:3000/teams")
.then(response => response.data);
}
}
export default model;
How can I access getAll()
method from another component ?
I tried importing model
and then referring it to getAll - model.getAll()
, but it complains that the method is undefined.
I tried referring to Calling a Function defined inside another function in Javascript , but could not find the solution.
Is this even the correct approach?
回答1:
You can't access getAll
from anywhere except inside model
. Maybe you meant to create an object?
var model = {
getAll: function () { ... }
}
model.getAll();
回答2:
You could always instantiate the function
function model() {
this.getAll = () => {
console.log("hello world");
};
}
const myFunc = new model();
myFunc.getAll() // console.log('hello world')
来源:https://stackoverflow.com/questions/52931016/how-to-access-nested-function-in-javascript-from-another-function