javascript ES6 Uncaught TypeError: Object(...) is not a function
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51374678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ES6 Uncaught TypeError: Object(...) is not a function
提问by izengod
I'm returning a Promisefrom this function
我正在Promise从这个函数返回一个
const liab_config = () => {
return axios.get(`${config.server_url}/fetch_config_liab`);
}
export default { liab_config }
And trying to handle the Promiseinside another file
并试图处理Promise另一个文件的内部
import liab_config from './utils/kc-adapter'
function set_liab_config(){
liab_config().then((response) => {
if(response.data.success){
let { kc_config_liab } = response.data;
return kc_config_liab['auth-server-url'];
}
else
return null;
}).catch(ex =>
console.log(ex));
}
Here I'm getting the error as:
在这里我得到的错误是:
Uncaught TypeError: Object(...) is not a function
未捕获的类型错误:Object(...) 不是函数
on line liab_config().then((response). What could be the reason?
在线liab_config().then((response)。可能是什么原因?
采纳答案by Bergi
You're default-exporting an object literal. You want to use either a named export
您正在默认导出对象文字。您想使用命名导出
const liab_config = …;
export { liab_config as liab_config }
// shorter:
const liab_config = …;
export { liab_config }
// or just:
export const liab_config = …;
with
和
import { liab_config } from './utils/kc-adapter'
or a default export
或默认导出
const liab_config = …;
export { liab_config as default }
// or just:
default export const liab_config = …;
// or without the local name:
default export …;
with
和
import liab_config from './utils/kc-adapter'
回答by Shubham Agarwal Bhewanewala
When you are accessing something like this
当你访问这样的东西时
import liab_config from './utils/kc-adapter'
import liab_config from './utils/kc-adapter'
It means you are asking for the default export which must be written like
这意味着您要求默认导出,必须这样写
const liab_config = () => {
return axios.get(`${config.server_url}/fetch_config_liab`);
}
export { liab_config as default };
or like this
或者像这样
const liab_config = () => {
return axios.get(`${config.server_url}/fetch_config_liab`);
}
export default liab_config;
And if you don't want to make it default then pass it like
如果您不想将其设为默认值,请像这样传递它
export const liab_config = () => {
return axios.get(`${config.server_url}/fetch_config_liab`);
}
or
或者
const liab_config = () => {
return axios.get(`${config.server_url}/fetch_config_liab`);
}
export { liab_config };
And access it like
并像访问它一样
import {liab_config} from './utils/kc-adapter'

