-1

我的 Angular HTTP GET 请求 indside clearNotifications() 在 notification.service.ts 没有命中 Express Route 路由/notifications.js。我从一个名为 app.component.ts 的组件中调用 clearNotifications()。我正在使用 Angular 7+

路线/通知.js

const router = require('express').Router();

//Additional modules
// const db = require('../config/database');
// const notificationModel = require('../models/notifications');

//Test connection
// db.authenticate().then(() => {
//     console.log('Connection has been established successfully.');
// }).catch(err => {
//     console.error('Unable to connect to the database:', err);
// });

//Clear all notifications
router.get('/clear', (req, res, next) => {
    console.log('clear');
    // notificationModel.destroy({});
});

module.exports = 路由器;

通知服务.ts

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class NotificationService {

  uri = 'http://localhost:5000'; 
  private socket = io(this.uri);

  constructor(private http: HttpClient) { }

  getNotification() {
    let observable = new Observable<{ string: String, number: String }>(observer => {
      this.socket.on('notification', (data) => {
        observer.next(data);
      });
      // return () => { this.socket.disconnect(); }
    })
    return observable;
  }

  clearNotifications() {
    return this.http.get(`${this.uri}/notifications/clear`);
  }
}

app.component.ts

import { Component } from '@angular/core';
import { NotificationService } from './notification.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [NotificationService]
})
export class AppComponent {
  title = 'client';

  string: String;
  number: String;
  notificationArray: Array<{ string: String, number: String }> = [];

  constructor(private notificationService: NotificationService) {
    this.notificationService.getNotification().subscribe(data => {
      this.notificationArray.push(data);
    });
  }

  clearNotifications() {
    this.notificationArray = [];
    this.notificationService.clearNotifications();
  }
}
4

2 回答 2

1

你应该这样做:检查express 的基本路由

var express = require('express');
var app = express();

app.get('/clear', (req, res) => {
    console.log('clear');

 res.send(success);
// notificationModel.destroy({});
});

还要确保使用subscribe组件中的服务方法。如果您不订阅,observables则不会执行。

你从哪里打电话clearNotifications

订阅clearNotifications组件,这将起作用: this.notificationService.clearNotifications().subscribe( (data) => { ..})

作为发布者,您创建一个定义订阅者函数的 Observable 实例。这是消费者调用该subscribe()方法时执行的函数。订阅者函数定义了如何获取或生成要发布的值或消息

于 2018-12-26T14:57:46.140 回答
0

angularhttp请求返回observable,所以你需要subscribe。如果没有任何订阅者observable,则不会执行。尝试

    clearNotifications() {
    return this.http.get(`${this.uri}/notifications/clear`)
               .subscribe(data => //your callback function,
                          error => // your error handler,
                          complete => // any after completion task);
  }
于 2018-12-26T14:55:43.857 回答