TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置

ngx-facebook


This is a wrapper for the official Facebook JavaScript SDK. It makes it easier to use Facebook SDK with Angular by providing components, providers and types.

Installation


1. Install via NPM:


  1. ``` shell
  2. npm i -S ngx-facebook
  3. ```

2. Add the Facebook JavaScript SDK to your index.html


  1. ``` html
  2. <script type="text/javascript" src="https://connect.facebook.net/en_US/sdk.js"></script>
  3. ```

3. Import FacebookModule into your app's root module


  1. ``` ts
  2. import { FacebookModule } from 'ngx-facebook';

  3. @NgModule({
  4.   ...
  5.   imports: [
  6.     FacebookModule.forRoot()
  7.   ],
  8.   ...
  9. })
  10. export class AppModule { }
  11. ```

4. Inject FacebookService and call the init method (optional):


This method must be called before using login or api methods. It is not required for other methods/components.

  1. ``` ts
  2. import { FacebookService, InitParams } from 'ngx-facebook';

  3. ...

  4. export class MyComponentOrService {

  5.   constructor(private fb: FacebookService) {

  6.     const initParams: InitParams = {
  7.       appId: '1234566778',
  8.       xfbml: true,
  9.       version: 'v2.8'
  10.     };

  11.     fb.init(initParams);

  12.   }

  13. }
  14. ```

Documentation


You can view complete and detailed documentation by visiting https://zyra.github.io/ngx-facebook/.

Example Usage


You can view our example project here and/or view its source code here

Example of login with Facebook


  1. ``` ts
  2. import { FacebookService, LoginResponse } from 'ngx-facebook';

  3. @Component(...)
  4. export class MyComponent {

  5.   constructor(private fb: FacebookService) { }

  6.   loginWithFacebook(): void {

  7.     this.fb.login()
  8.       .then((response: LoginResponse) => console.log(response))
  9.       .catch((error: any) => console.error(error));
  10.   }

  11. }
  12. ```

Example of sharing on Facebook


  1. ``` ts
  2. import { FacebookService, UIParams, UIResponse } from 'ngx-facebook';

  3. ...

  4. share(url: string) {

  5.   const params: UIParams = {
  6.     href: 'https://github.com/zyra/ngx-facebook',
  7.     method: 'share'
  8.   };

  9.   this.fb.ui(params)
  10.     .then((res: UIResponse) => console.log(res))
  11.     .catch((e: any) => console.error(e));

  12. }
  13. ```

Example of adding a Facebook like button


  1. ``` html
  2. <fb-like href="https://github.com/zyra/ngx-facebook"></fb-like>
  3. ```

Example of playing a Facebook video


Basic video component usage:


  1. ``` html
  2. <fb-video href="https://www.facebook.com/facebook/videos/10153231379946729/"></fb-video>
  3. ```

Advanced video component usage:


  1. ``` html
  2. <fb-video href="https://www.facebook.com/facebook/videos/10153231379946729/" (paused)="onVideoPaused($event)"></fb-video>
  3. ```

  1. ``` ts
  2. import { Component, ViewChild } from '@angular/core';
  3. import { FBVideoComponent } from 'ngx-facebook';

  4. @Component(...)
  5. export class MyComponent {

  6.   @ViewChild(FBVideoComponent) video: FBVideoComponent;

  7.   ngAfterViewInit() {
  8.     this.video.play();
  9.     this.video.pause();
  10.     this.video.getVolume();
  11.   }

  12.   onVideoPaused(ev: any) {
  13.     console.log('User paused the video');
  14.   }

  15. }
  16. ```

Versioning


We use SemVer for versioning. For the versions available, see the tags on this repository.

Contribution


Having an issue? or looking for support? Open an issue and we will get you the help you need.
Got a new feature or a bug fix? Fork the repository, make your changes, and submit a pull request.

License


This project is licensed under the MIT License - see the LICENSE.md file for details

Support this project


If you find this project useful, please star the repository to let people know that it's reliable. Also, share it with friends and colleagues that might find this useful as well. Thank you 😄
Last Updated: 2023-09-03 17:10:52