如何在脚本标签中使用导出的函数?

huangapple go评论101阅读模式
英文:

How to use an exported function in a script tag?

问题

我可以帮你翻译代码部分。以下是代码的翻译:

export function add(num1, num2) {
    const errorMessage = "请输入有效的数字";
    if (isNaN(num1) || isNaN(num2)) {
        return errorMessage;
    }
    return num1 + num2;
}
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="ISO-8859-1">
    <title>示例</title>
    <script type="module" src="file.js"></script>
</head>
<body>
<div>
    <input type="number" id="num1" placeholder="第一个数字">
    <input type="number" id="num2" placeholder="第二个数字">
    <button onclick="addNumbers()">相加</button>
    <p id="additionResult"></p>
</div>

<script>

    function addNumbers() {
        const num1 = parseFloat(document.getElementById('num1').value);
        const num2 = parseFloat(document.getElementById('num2').value);
        const result = add(num1, num2)
        document.getElementById('additionResult').innerText = `加法结果: ${result}`;
    }
</script>
</body>
</html>

希望这能帮助你解决问题!

英文:

How can I use my export function in my html script tag. I have marked the function as export so that I can use it in my Jasmine UnitTests. However, I can no longer use my function in my html script tag and get the following error message:
Uncaught ReferenceError: add is not defined

Is there any way I can solve or work around this problem?

My file.js:

export function add(num1, num2) {
    const errorMessage = &quot;Please enter a valid number&quot;;
    if (isNaN(num1) || isNaN(num2)) {
        return errorMessage;
    }
    return num1 + num2
}

My index.html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;de&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;ISO-8859-1&quot;&gt;
    &lt;title&gt;Sample&lt;/title&gt;
    &lt;script type=&quot;module&quot; src=&quot;file.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
    &lt;input type=&quot;number&quot; id=&quot;num1&quot; placeholder=&quot;Erste Zahl&quot;&gt;
    &lt;input type=&quot;number&quot; id=&quot;num2&quot; placeholder=&quot;Zweite Zahl&quot;&gt;
    &lt;button onclick=&quot;addNumbers()&quot;&gt;Addiere&lt;/button&gt;
    &lt;p id=&quot;additionResult&quot;&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;script&gt;

    function addNumbers() {
        const num1 = parseFloat(document.getElementById(&#39;num1&#39;).value);
        const num2 = parseFloat(document.getElementById(&#39;num2&#39;).value);
        const result = add(num1, num2)
        document.getElementById(&#39;additionResult&#39;).innerText = `Ergebnis der Addition: ${result}`;
    }
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

My Unittest:

import {add} from &#39;../file.js&#39;

describe(&#39;MyTest&#39;, () =&gt; {

    beforeEach(() =&gt; {
    });

    describe(&quot;add Funktion&quot;, function () {
        it(&quot;sollte zwei Zahlen korrekt addieren&quot;, function () {
            expect(add(2, 3)).toEqual(5);
            expect(add(0, 0)).toEqual(0);
            expect(add(-1, 1)).toEqual(0);
        });

        it(&quot;sollte eine Fehlermeldung zur&#252;ckgeben, wenn ung&#252;ltige Eingaben gemacht werden&quot;, function () {
            expect(add(&quot;a&quot;, 2)).toEqual(&quot;Bitte geben Sie g&#252;ltige Zahlen ein.&quot;);
            expect(add(1, &quot;b&quot;)).toEqual(&quot;Bitte geben Sie g&#252;ltige Zahlen ein.&quot;);
            expect(add(&quot;abc&quot;, &quot;def&quot;)).toEqual(&quot;Bitte geben Sie g&#252;ltige Zahlen ein.&quot;);
        });

        it(&quot;sollte eine Fehlermeldung zur&#252;ckgeben, wenn eine Eingabe fehlt&quot;, function () {
            expect(add(2)).toEqual(&quot;Bitte geben Sie g&#252;ltige Zahlen ein.&quot;);
            expect(add()).toEqual(&quot;Bitte geben Sie g&#252;ltige Zahlen ein.&quot;);
        });
    });
});

If anyone has an answer I would be very pleased for a detailed response, thanks for any help!

答案1

得分: 2

Set type="module" on the script that uses the add function and import the function from file.js.

<script type="module">
import { add } from './file.js';
function addNumbers() {
    // ...
}
document.querySelector('button').addEventListener('click', addNumbers);
</script>
英文:

Set type=&quot;module&quot; on the script that uses the add function and import the function from file.js.

&lt;script type=&quot;module&quot;&gt;
import { add } from &#39;./file.js&#39;;
function addNumbers() {
    // ...
}
document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, addNumbers);
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年6月26日 01:19:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定