-3

我正在开发一个 Blazor 服务器项目,并且在我的组件中有这个按钮

    <button type="button" class="btn btn-primary" @onclick="@GetUserInput">Add Name</button>    

这是我拥有的点击事件正在调用的代码

    protected async void GetUserInput()
            {
                var result = await JsRuntime.InvokeAsync<string>("getName");
            }

    function getName() {
        var retVal = prompt("Enter Name : ", "name here");
        return retval;
    }

在 javascript 函数中,retVal 有一个值,但在 GetUserInput 方法中,代码崩溃并显示以下错误消息

    ReferenceError: retval is not defined
        at getActivityName (https://localhost:44318/script.js:15:11)
        at https://localhost:44318/_framework/blazor.server.js:1:70045
        at new Promise (<anonymous>)
        at e.beginInvokeJSFromDotNet (https://localhost:44318/_framework/blazor.server.js:1:70011)
        at https://localhost:44318/_framework/blazor.server.js:1:26293
        at Array.forEach (<anonymous>)
        at e.invokeClientMethod (https://localhost:44318/_framework/blazor.server.js:1:26263)
        at e.processIncomingData (https://localhost:44318/_framework/blazor.server.js:1:24201)
        at e.connection.onreceive (https://localhost:44318/_framework/blazor.server.js:1:17286)
        at WebSocket.i.onmessage (https://localhost:44318/_framework/blazor.server.js:1:46503)'

我不确定发生了什么?有任何想法吗?

4

1 回答 1

1

Javascript 区分大小写。retVal,而不是 retval - JS 不会抱怨“retval” - 它只是未定义。

function getName() {
        var retVal = prompt("Enter Name : ", "name here");
        return retVal;
    }
于 2021-06-10T20:56:35.987 回答