Javascript 为什么从一个页面转到另一个页面时,信号器函数仍然存在于DOM范围中-单页面应用程序
我创建了一个单页应用程序,这是我的第一个,所以我可能走错了路 我正在使用母版页,其中我正在加载另外两个页面。此外,我还使用javascript模块,以便使用私有变量和函数 在母版页或Mail.html中,我启动信号器连接,并初始化模块。 在收件箱子页面中,我正在等待来自服务器的“邮件”,在发件箱中,我正在显示所有已发送的邮件。为了实现这一点,我使用了两个不同的模块(收件箱和发件箱) 在这种情况下,当我加载inbox.html后加载第二个页面(在本例中为发件箱)时,就会出现我的问题 signalr仍然工作,这很奇怪,因为该连接仅在该模块的收件箱页面中使用 我尝试了很多方法来防止这种情况,比如在加载下一个页面之前卸载当前页面,我还尝试删除脚本,只在收件箱页面中启动连接,但没有任何帮助。 另外,在加载Outbox.html时,我找不到这个函数在DOM中存储的确切位置 这是我的密码:Javascript 为什么从一个页面转到另一个页面时,信号器函数仍然存在于DOM范围中-单页面应用程序,javascript,jquery,signalr,Javascript,Jquery,Signalr,我创建了一个单页应用程序,这是我的第一个,所以我可能走错了路 我正在使用母版页,其中我正在加载另外两个页面。此外,我还使用javascript模块,以便使用私有变量和函数 在母版页或Mail.html中,我启动信号器连接,并初始化模块。 在收件箱子页面中,我正在等待来自服务器的“邮件”,在发件箱中,我正在显示所有已发送的邮件。为了实现这一点,我使用了两个不同的模块(收件箱和发件箱) 在这种情况下,当我加载inbox.html后加载第二个页面(在本例中为发件箱)时,就会出现我的问题 signalr
Mail.html
<html>
<head>
//scripts
//css
</head>
<body>
<div class="page" id="Inbox">Inbox</div>
<div class="page" id="Outbox">Outbox</div>
<div id="loadPage">
</div>
<script type="text/javascript">
var SignalRHub = $.connection.hubs;
$.connection.hub.start();
var MailModule = {};
$('body').off('click', '.page').on('click', '.page', function () {
MailModule = {};
if ($(this).attr('id') == 'Inbox') {
$("#loadPage").empty().load("/Home/Inbox", function () {
MailModule.Inbox.Init();
});
}
if ($(this).attr('id') == 'Outbox') {
$("#loadPage").empty().load("/Home/Outbox", function () {
MailModule.Outbox.Init();
});
}
});
</script>
<body>
</html>
<div id="items"></div>
<script>
(function (mail, $) {
mail.Inbox = (function () {
var ReceiveMail = function () {
// This is still active when there is new 'mail' from server
// even though I'm not on this page
SignalRHub.client.receiveMail = function (UserName, Title){
$("#items").append("<p>"+UserName+"-"+Title+"</p>");
}
};
var init = function () {
ReceiveMail();
};
return {
Init: init
};
}());
}(window.MailModule = window.MailModule || {}, jQuery));
</script>
<div id="items"></div>
<script>
(function (mail, $) {
mail.Outbox = (function () {
var GetSend = function () {
$.get("URL", { UserId: UserId }, function (result) {
$("#items").append("<p>"+result[0].UserName+"-"+result[0].Title+"</p>");
}
};
var init = function () {
GetSend();
};
return {
Init: init
};
}());
}(window.MailModule = window.MailModule || {}, jQuery));
</script>
Outbox.html
<html>
<head>
//scripts
//css
</head>
<body>
<div class="page" id="Inbox">Inbox</div>
<div class="page" id="Outbox">Outbox</div>
<div id="loadPage">
</div>
<script type="text/javascript">
var SignalRHub = $.connection.hubs;
$.connection.hub.start();
var MailModule = {};
$('body').off('click', '.page').on('click', '.page', function () {
MailModule = {};
if ($(this).attr('id') == 'Inbox') {
$("#loadPage").empty().load("/Home/Inbox", function () {
MailModule.Inbox.Init();
});
}
if ($(this).attr('id') == 'Outbox') {
$("#loadPage").empty().load("/Home/Outbox", function () {
MailModule.Outbox.Init();
});
}
});
</script>
<body>
</html>
<div id="items"></div>
<script>
(function (mail, $) {
mail.Inbox = (function () {
var ReceiveMail = function () {
// This is still active when there is new 'mail' from server
// even though I'm not on this page
SignalRHub.client.receiveMail = function (UserName, Title){
$("#items").append("<p>"+UserName+"-"+Title+"</p>");
}
};
var init = function () {
ReceiveMail();
};
return {
Init: init
};
}());
}(window.MailModule = window.MailModule || {}, jQuery));
</script>
<div id="items"></div>
<script>
(function (mail, $) {
mail.Outbox = (function () {
var GetSend = function () {
$.get("URL", { UserId: UserId }, function (result) {
$("#items").append("<p>"+result[0].UserName+"-"+result[0].Title+"</p>");
}
};
var init = function () {
GetSend();
};
return {
Init: init
};
}());
}(window.MailModule = window.MailModule || {}, jQuery));
</script>
(功能(邮件,$){
mail.Outbox=(函数(){
var GetSend=函数(){
$.get(“URL”,{UserId:UserId},函数(结果){
$(“#项”)。追加(“”+结果[0]。用户名+“-”+结果[0]。标题+””;
}
};
var init=函数(){
GetSend();
};
返回{
Init:Init
};
}());
}(window.MailModule=window.MailModule | |{},jQuery));
注意:可能这种模式不正确,如果您能给我一个如何实现更好的模式的线索,我将不胜感激。单页应用程序不会刷新页面,因此以前运行的JavaScript仍然存在。这就是SPA的工作方式。什么是信号器仍然工作?它仍然连接吗?断开连接如何?$。连接.hub.stop();在加载第二个页面后?是的,也许这样可以解决问题,但我不能这样做,因为我仍然需要该连接才能实时“转发”消息。。