国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Wie erhalte ich beim Import den Rückgabewert ?Export Default'?
P粉595605759
P粉595605759 2023-08-27 23:36:46
0
1
728
<p>Einfach gesagt, ich habe eine <code>js</code>-Datei, deren Exportstandards ein Objekt zurückgeben. </p> <pre class="brush:js;toolbar:false;">// x.js export default ()=>({ Text: 'Text' }) </pre> <p>Ich m?chte es in eine andere <code>js</code>-Datei importieren und mit seinen Daten zusammenführen (sozusagen erweitern). Jetzt mache ich Folgendes: </p> <pre class="brush:js;toolbar:false;">// y.js x aus './x.js' importieren; const obj = x() export default ()=>({ ...obj, text2: "text2" }) </pre> <p>Es funktioniert, aber es ist nicht sauber. Gibt es einen einfacheren Weg, dies zu tun? </p>
P粉595605759
P粉595605759

Antworte allen(1)
P粉715304239

我認(rèn)為,“我想使用一種干凈的方法”,這是一種易于理解的方法。因此,以下方法可能有用 -

1。默認(rèn)導(dǎo)出-
這對于僅導(dǎo)出單個對象、函數(shù)或變量非常有用。在導(dǎo)入過程中,我們可以使用任何名稱來導(dǎo)入。

// x.js
export default function xFunc() {
    return { text: "text" };
}

//y.js
import xFunc from "./x.js";
export default function yFunc() {
    return {
        ...xFunc(),
        text2: "text2",
    };
}

// import y in any file
import yFunc from "./y";
console.log(yFunc());

默認(rèn)導(dǎo)出也可以這樣使用-
這很有用,因為我們可以使用任何名稱來娛樂,因為它是帶有名稱的默認(rèn)導(dǎo)出(因此我們可以記住該名稱)并使用任何名稱導(dǎo)入。

// x.js
function xFunc() {
  return { text: "text" };
}
export { xFunc as default };

// y.js
import anyName from "./x.js";
function yFunc() {
  return {
    ...anyName(),
    text2: "text2",
  };
}
export { yFunc as default };

// import y in any file
import anyName from "./y";
console.log(anyName());

2。命名導(dǎo)出(推薦)-
這對于導(dǎo)出多個值很有用。導(dǎo)入過程中,必須使用相同的名稱,以避免導(dǎo)出和導(dǎo)入名稱之間的混淆。

// x.js
export const xFunc = () => { text: "text" };

//y.js
import { xFunc } from "./x.js";
export const yFunc = () => {
  return {
    ...xFunc(),
    text2: "text2",
  };
};

// import y in any file
import { yFunc } from "./y";
console.log(yFunc());

編輯--

命名導(dǎo)出也可以這樣使用(不使用 const 和箭頭函數(shù))

// x.js
function xFunc() {
  return { text: "text" };
}
export { xFunc };

//y.js
import { xFunc } from "./x.js";
function yFunc() {
  return {
    ...xFunc(),
    text2: "text2",
  };
}
export { yFunc };

// import y in any file
import { yFunc } from "./y";
console.log(yFunc());
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage