我正在使用angular-cli。 我正在尝试实现这个例子:
https://www.npmjs.com/package/angular2-websocket-service
我已经创建了我的服务,我想直接在AppComponent中使用它。
这就是我所拥有的:
应用/ app.module.ts
price_index = pandas.read_csv("houses_price_index_losangeles.csv")
price_index['Los Angeles']
price = price_index['Los Angeles']
month = price_index['Month-Year']
plt.plot(price,month,'g',label='Tarzana')
plt.title('Houses for sale')
plt.ylabel('Prices')
plt.xlabel('Year')
plt.legend()
plt.grid(True,color='k')
plt.show()
应用/ app.component.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { WebSocketService } from 'angular2-websocket-service'
import { MyWebsocketService } from './websocket/mywebsocket.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [
WebSocketService,
MyWebsocketService
],
bootstrap: [AppComponent]
})
export class AppModule { }
应用/网页套接字/ mywebsocket.service.ts
import { Component, OnInit } from '@angular/core';
import {MyWebsocketService} from './websocket/mywebsocket.service'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'title';
constructor(private mywebsocket: MyWebsocketService) { }
ngOnInit() {
this.mywebsocket.connect();
}
}
这就是我得到的:
import { Injectable } from '@angular/core'; import { WebSocketService } from 'angular2-websocket-service' import { Observable } from 'rxjs/Observable' import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/operator/share' @Injectable() export class MyWebsocketService { private q: Observable<any> private w: Observable<any> constructor(private socketFactory: WebSocketService) { } public connect() { this.q = new Observable<any>() this.w = this.socketFactory.connect('ws://localhost:8080/myapp/cswebsocket', this.q).share() this.w.subscribe() } }
为什么这个未定义?我该如何解决这个问题?