TL;DR
I inserted the exact code that the 
The Goal
In our Angular 12 Application, we want to include three things under a video:
- Facebook Like Button
- Facebook Share Button
- Pinterest Pin Button
I created components for each of them and they trigger writing a script Tag to the DOM to fetch the JS Libraries (we use NGXS for asynchronous actions).
facebook-share-button.component.ts
const SCRIPT_ID = 'facebook-share-button';
@Component(...)
export class FacebookShareButtonComponent implements AfterViewInit, OnDestroy {
@Select((state) => state.router.state.fullUrl) fullUrl$: Observable<string>;
constructor(private store: Store) {}
ngAfterViewInit() {
this.store.dispatch(
new CoreActions.LoadScript(
SCRIPT_ID,
'https://connect.facebook.net/de_DE/sdk.js#xfbml=1&version=v12.0',
{ async: true }
)
);
}
ngOnDestroy() {
this.store.dispatch(new CoreActions.RemoveScript(SCRIPT_ID));
}
}
facebook-share-button.component.html
<div >
<div id="fb-root"></div>
<div
[attr.data-href]="fullUrl$ | async"
data-layout="button"
data-size="large"
></div>
</div>
The Problem
The Facebook Share Button and Pinterest Pin Button work just fine, only the Facebook Like Button does not show. It has width and height of 0px
Full code
facebook-like-button.component.ts
import { AfterViewInit, Component, OnDestroy } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { CoreActions } from 'src/app/core/core.actions';
const SCRIPT_ID = 'facebook-like-button';
@Component({
selector: 'app-facebook-like-button',
templateUrl: './facebook-like-button.component.html',
styleUrls: ['./facebook-like-button.component.styl'],
})
export class FacebookLikeButtonComponent implements AfterViewInit, OnDestroy {
@Select((state) => state.router.state.fullUrl) fullUrl$: Observable<string>;
constructor(private store: Store) {}
ngAfterViewInit() {
this.store.dispatch(
new CoreActions.LoadScript(
SCRIPT_ID,
'https://connect.facebook.net/de_DE/sdk.js#xfbml=1&version=v12.0',
{ anonymous: true, nonce: '4j5id3DJ' }
)
);
}
ngOnDestroy() {
this.store.dispatch(new CoreActions.RemoveScript(SCRIPT_ID));
}
}
facebook-like-button.component.html
<div >
<div id="fb-root"></div>
<div
[attr.data-href]="fullUrl$ | async"
data-width="77"
data-layout="standard"
data-action="like"
data-size="large"
data-share="false"
></div>
</div>
Things I tried
- Removing the facebook-share-button Component (maybe the two scripts are interfering)
- Placing the code in
ngOnInit()(instead ofngAfterViewInit()) - Setting a static
data-hrefAttribute (i.e. removing the async-Pipe) - Removing
<div id="fb-root"></div>infacebook-share-button.component.htmlorfacebook-like-button.component.htmlor both - Moving
<div id="fb-root"></div>toapp.component.html - Playing around with the options of
.fb-likeand removing them one by one - Setting
async,deferandcrossoriginto different values in the script tag - Other browsers and deactivating AdBlockers
- Manually include the script tags
- Search on Stackoverflow for people with simlar problems
- And a combination of all of the above
but nothing worked.



