在Angular 4中使用mongodb-stitch库 [英] Using mongodb-stitch library in Angular 4

查看:67
本文介绍了在Angular 4中使用mongodb-stitch库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Angular中尝试MongoDB Stitch服务,到目前为止,我仍然可以使用该服务.但是,我可以连接到服务的唯一方法是在HTML页面上包含AWS托管的js库.

Im been trying the MongoDB Stitch service in Angular, so far Im able to use the service. However, the only way I could connect to the service is by including the js library hosted in AWS on the html page.

有一个可用的mongodb-stitch npm软件包,并且在mongodb教程中有一些有关如何使用它的示例页面.但这是一个纯JS库(不支持TS),我尝试了几种方法(使用require,安装lib的类型(不可用),使用@types)无济于事.

There is a mongodb-stitch npm package available and there are sample pages on mongodb tutorial on how to use it. But this is a pure JS library (no TS support) and I have tried several ways (using require, installing typings of the lib (not available), using @types) to no avail.

有人在Ng4上尝试过吗?很乐意按照您使用"mongodb-stitch"程序包创建服务的步骤进行操作.

Anyone tried this on Ng4? Would love to have the steps you did to use the 'mongodb-stitch' package the create a service.

推荐答案

另一个答案建议实例化StitchClient的新实例,MongoDB在

The other answer suggests instantiating a new instance of StitchClient which is something that MongoDB have explicitly advised against in the Official API Documentation - and with reason, since there is a factory method available for that purpose. So, (after installing mongodb-stitch), the following code would help you get started in component.ts

import { Component, OnInit } from "@angular/core";
import { StitchClientFactory } from "mongodb-stitch";

let appId = 'authapp-****';

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

export class MongoAuthComponent implements OnInit {

mClient;

ngOnInit() {
  this.mClient = StitchClientFactory.create(appId);
}

然后您就可以将其用于任何您想要的目的,例如用于通过Google实施登录

And you can then use this for whatever purpose you want, such as for implementing sign-in with Google

gLogin(){
this.mClient.then(stitchClient => {
  stitchClient.authenticate("google");
})

这篇关于在Angular 4中使用mongodb-stitch库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆