【问题标题】:Could not find property of window when doing JS interop with Blazor与 Blazor 进行 JS 互操作时找不到窗口的属性
【发布时间】:2019-06-09 11:08:22
【问题描述】:

您好,我正在尝试从Blazorjs 文件调用一个方法。 我的文件结构是这样的:

-root
  -JSInterop.cs
  -js(folder)
    -meth.js  (file containing the js method)

我不断收到以下错误:

 Could not find 'methods' in 'window'.

**调用js的Cs类**

public class JSInterop {
        public static async Task<string> ChangeText() {
            try {
                var data = await JSRuntime.Current.InvokeAsync<string>("./js/meth/methods.print","mymessage");
                Console.WriteLine($"ReturnedFromJS:{data}");
                return data;
            } catch (Exception ex) {

                return ex.Message;
            }

        }
    }

Js 文件

function print(message){
 return "fromJs"+message;
}

window.methods = {
    print: function (message) {
        return "from js" + message;
    }
}

我已经尝试将方法和作为属性放在window 中。我不确定在第一种情况下如何在 js 中引用文件中的方法。

 "[path to file]/[containingfile]/[methodname]" ?
  or i have also tried "[path to file] / window.[methodname]"

无济于事(在第二种情况下)

Index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width">
    <title>Sms.Studio.Web</title>
    <base href="/" />
    <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
    <link href="css/site.css" rel="stylesheet" />
</head>
<body>
    <app>Loading...</app>

    <!-- browser -->
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="../interop/js/meth.js"></script>

</body>
</html>

【问题讨论】:

    标签: javascript c# interop blazor


    【解决方案1】:

    JSRuntime.Current.InvokeAsync 将相对于全局 window 范围的 js 函数标识符作为其第一个参数。所以在你的 js 文件中你可能有:

    window.methods = {
        print: function (message) {
        return "from js" + message
    }
    

    在 index.html 中添加你的 js 文件

    <script src="css/bootstrap/bootstrap-native.min.js"></script>
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="js/meth.js"></script>
    

    并从 .Net 中调用它,如下所示

    await JSRuntime.Current.InvokeAsync<string>("methods.print","mymessage");
    

    【讨论】:

    • 我更新了我的答案,我已经在index.html 中添加了Intellisense 帮助的路径。
    【解决方案2】:
    // Try this:
    // Don't call your class JSInterop
    public class MyJSInterop {
            public static async Task<string> ChangeText() {
                try {
                    var data = await JSRuntime.Current.InvokeAsync<string>("methods.print","mymessage");
                    Console.WriteLine($"ReturnedFromJS:{data}");
                    return data;
                } catch (Exception ex) {
    
                    return ex.Message;
                }
    
            }
        }
    
    // Js file
    window.methods = {
        print: function (message) {
            return "from js" + message;
        }
    };
    

    【讨论】:

    • 我最初尝试在我的cshtml 文件中使用JSRuntime.Current.InvokeAsync 并出现window 的此错误。当我将JS 调用委托给另一个类时,就像你在这里所做的@ 987654326@我在js文件中得到一个控制台错误Uncaught SyntaxError: Unexpected token &lt;
    • JavaScript return 语句中缺少分号,可能导致语法错误...
    • 你还需要这样的分号:window.methods = {}***;***
    • 我不明白。我在return 之后放置了一个分号,并且在window.....=... ; 声明之后也尝试了。仍然收到此错误。
    【解决方案3】:

    以下是编写 cookie 的端到端示例。

    第 1 步 - 添加 MatButton 并将其 onClick 属性设置为 delegate

    <MatButton TrailingIcon="favorite" @onclick="@(async () => await AddItemtoShoppingCart(@item))" Label="add"></MatButton>
    

    第 2 步

    @code{
    public async Task AddItemtoShoppingCart(FoodItem selectedItem)
        {
            var test = await JSRuntime.InvokeAsync<object>("blazorExtensions.WriteCookie", "cookieName", "cookieValue", "cookieExpiryDate");
           
        }
    
    }
    

    第 3 步 - 在 javasceipt 下方添加_Host.cshtml

    <script>
            window.blazorExtensions = {
    
                WriteCookie: function (name, value, days) {
    
                    var expires;
                    if (days) {
                        var date = new Date();
                        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                        expires = "; expires=" + date.toGMTString();
                    }
                    else {
                        expires = "";
                    }
                    document.cookie = name + "=" + value + expires + "; path=/";
                }
            }
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      相关资源
      最近更新 更多