【问题标题】:How to work with external JS file in Signal r如何在 Signal r 中使用外部 JS 文件
【发布时间】:2016-02-03 21:02:40
【问题描述】:

我的剃刀局部视图上有以下信号 r 客户端代码 它在局部视图中工作正常。我想将其移动到外部 javascript 文件中。

<script type="text/javascript">
        $(function () {
            debugger;
            $.connection.hub.logging = true;
            var proxy = $.connection.broadcastMessage;

            proxy.client.receiveNotification = function (message, count) {
                debugger;
                $("#not_count").html(count);
                $("#not_count").show();
       };
  $.connection.hub.start();
});
</script>

如何将其移至外部 JS 文件?

【问题讨论】:

  • 那么从视图中剪切这段代码有什么问题,粘贴到一些外部文件中,然后将此文件包含为&lt;script src = "path_to_the_file"&gt;&lt;/script&gt;

标签: javascript asp.net-mvc razor signalr signalr.client


【解决方案1】:

使用您的代码将新的 javascript 文件(例如 signalrStuff.js)添加到解决方案中:

$(function () { debugger; $.connection.hub.logging = true; var proxy = $.connection.broadcastMessage;

            proxy.client.receiveNotification = function (message, count) {
                debugger;
                $("#not_count").html(count);
                $("#not_count").show();
       };
  $.connection.hub.start();
});

然后在局部视图中引用替换javascript的新脚本:

<script src="signalrStuff.js" type="text/javascript"></script>

确保首先加载 jQuery 脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2019-11-07
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2021-05-24
    • 2018-11-23
    相关资源
    最近更新 更多