可以尝试在Angular项目中配置WebSocket端点以解决实时重载无法工作的问题。下面是一些示例代码来帮助您实现此操作:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { WebSocketModule, WebSocketService } from 'angular2-websocket';
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
WebSocketModule
],
declarations: [AppComponent],
providers: [WebSocketService],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private _websocket: WebSocketService) {
this._websocket.connect('ws://localhost:8080');
}
}
import { Component } from '@angular/core';
import { WebSocketService } from 'angular2-websocket';
@Component({
selector: 'app-root',
template: `
WebSocket Example
- {{ message }}
`
})
export class AppComponent {
messages: string[] = [];
constructor(private _websocket: WebSocketService) {
this._websocket.connect('ws://localhost:8080');
this._websocket.onMessage((data: any) => {
this.messages.push(data);
window.location.reload();
});
}
}
通过以上步骤,在Angular项目中配置WebSocket端点,即可解决实时重载无法工作的问题。