【问题标题】:Uncaught Error: jQuery was not found. Please ensure jQuery is referenced before the SignalR client JavaScript file未捕获的错误:未找到 jQuery。请确保在 SignalR 客户端 JavaScript 文件之前引用 jQuery
【发布时间】:2017-08-03 20:05:48
【问题描述】:

我正在尝试使用 SignalR 实现应用程序,但出现此错误,我查看了 stackoverflow 中的其他类似问题,但我相信我的不同。

我已经安装了 jquery,signalr。

npm install jquery signalr --save

并导入到我的组件中。

import { Component } from '@angular/core';
import * as $ from "jquery"; 
import "signalr";

export class AppComponent {

   private connection: any;

    //signalR proxy reference
    private proxy: any;

    private jQuery = $;
    constructor() {
      this.connection = $.hubConnection("http://www.example.com");  
        // create new proxy as name already given in top  
        this.proxy = this.connection.createHubProxy('SignalRHub'); 
    }
}

同样在index.html中,顺序是正确的。

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.1.min.js"></script>

【问题讨论】:

  • import * as $ from "jquery";在顶部导入 jquery
  • 谢谢,但它已经在上面了。
  • 在角度之前,这是一个猜测
  • 我也试过了,同样的错误。

标签: angular signalr


【解决方案1】:

我已经解决了定义 jQuery 变量的问题。这是工作版本,以防万一它可能对某人有所帮助。

import { Component } from '@angular/core';
import "jquery"; 
import "signalr";

declare var jQuery:any;
declare var $:any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {

   private connection: any;

  //signalR proxy reference
  private proxy: any;

  private jQuery = $;
  constructor() {
    // debugger;
    this.connection = $.hubConnection("http://wwwexample.com/api");  
    // create new proxy as name already given in top  
    this.proxy = this.connection.createHubProxy('SignalRHub'); 
    this.proxy.on('messageReceived', (latestMsg) => this.onMessageReceived(latestMsg));

    this.connection.start();

    // this.broadcastMessage("test");
  }

  private onMessageReceived(latestMsg: string) {
    debugger;
      console.log('New message received: ' + latestMsg);
  }

  //method for sending message
  broadcastMessage(msg: string) {
      //invoke method by its name using proxy 
      // this.proxy.invoke('GetRealTime', msg);
      this.proxy.invoke('Hello');
  }    
}

【讨论】:

    猜你喜欢
    • 2016-01-11
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多