我想匯出默認匯出默認值中的所有函式,但是每次嘗試將它們匯入另一個檔案時,都會收到函式不存在的錯誤。我匯入錯了嗎?
export default () => {
// methods
const makeRequest = async () => {
async function useAllBenefits() {
...
return payload;
}
async function useTopBenefits() {
...
return payload;
}
// exposed
return {
useAllBenefits,
useTopBenefits,
makeRequest
};
};
}
myotherfile.js
import all from '../myFile'
console.log(all.useAllBenefits)
uj5u.com熱心網友回復:
- 不要將它們放在另一個函式中
- 使用
export關鍵字
這樣的:
export const aFunction = () => {};
export const anOtherFunction = () => {};
const someDefaultExport = () => {};
export default someDefaultExport;
然后你可以匯入它們:
import theDefault, { aFunction, anOtherFunction } from "../myFile";
uj5u.com熱心網友回復:
如果要將多個函式匯出為 DEFAULT,您可以通過匯出一個屬性為函式的物件來實作。
export default {
function1: () => {},
function2: () => {},
function3: () => {}
}
現在在另一個檔案中,您可以匯入它們并如下使用:
import all from '../myFile.js';
all.function1();
all.function2();
uj5u.com熱心網友回復:
方法一、匯出各個函式
myFunction.js
export const sum = (a,b) {
return a b;
}
export const multiply = (a,b) {
return a * b;
}
方法二。
myFunction.js
const sum = (a,b) {
return a b;
}
const multiply = (a,b) {
return a * b;
}
export default {sum, multiply}
匯入部分
檔案.js
import all from './myFunction.js'
all.sum(10, 11);
或者 file.js
import {sum, multiply} from './myFunction.js'
sum(10, 11);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381392.html
標籤:javascript 打字稿
