В Angular 5 (CLI) как передать данные из index.js в компонент? - PullRequest
0 голосов
/ 19 мая 2018

У меня есть некоторый код в index.js, который я получил из этого урока: https://angularfirebase.com/lessons/angular-stripe-payments-part-2-firebase-cloud-functions-backend/

Вот код:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp(functions.config().firebase);

const stripe = require('stripe')(functions.config().stripe.testkey)



exports.stripeCharge = functions.database
                                .ref('/payments/{userId}/{paymentId}')
                                .onWrite(event => {



  const payment = event.data.val();
  const userId = event.params.userId;
  const paymentId = event.params.paymentId;


  // checks if payment exists or if it has already been charged
  if (!payment || payment.charge) return;

  return admin.database()
              .ref(`/users/${userId}`)
              .once('value')
              .then(snapshot => {
                  return snapshot.val();
               })
               .then(customer => {

                 const amount = payment.amount;
                 const idempotency_key = paymentId;  // prevent duplicate charges
                 const source = payment.token.id;
                 const currency = 'usd';
                 const charge = {amount, currency, source};


                 return stripe.charges.create(charge, { idempotency_key });

               })

               .then(charge => {
                   admin.database()
                        .ref(`/payments/${userId}/${paymentId}/charge`)
                        .set(charge)
                  })


});

Как передать сообщение «УСПЕХ»мой компонент, например: make-payment.component.ts, который находится в TypeScript.

1 Ответ

0 голосов
/ 19 мая 2018

Я не знаю, является ли лучший способ сделать это, но вы можете использовать пользовательское Событие для связи с вашим Компонентом

На вашем Компоненте или что-либо еще:

import { Component } from '@angular/core';
import {fromEvent} from 'rxjs';
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      constructor() {
        fromEvent(window,'myEvent').subscribe((e:any) => {
            console.log(e.detail);
        });
      }
    }

И выходвашего углового приложения (пример здесь: index.ts, но может быть где угодно)

platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
  // create and dispatch the event
  var event = new CustomEvent("myEvent", {
    detail: {
      foo: 'bar'
    }
  });
  // be sure to dispatch event after you add listener.
  window.dispatchEvent(event);
}).catch(err => console.error(err));

онлайн-код здесь

...