-
With the Angular CLI, create a
LandingPageComponent
or Any other page component that you want to be private:ng g c LandingPage
-
Add the below imports to
app.module.ts
import { HttpClientModule } from '@angular/common/http'; @NgModule({ declarations: [ LandingPageComponent, ... ], imports: [ ... HttpClientModule, BuilderModule.forRoot('Builder-Public-API-Key') ], providers: [], bootstrap: [AppComponent] })
-
Generate Builder Private key
-
Go over to builder.io/models and choose your model and hit
show more options
undermodel settings
and then find thepublic readable
option and turn that off and save your model, e.g. perhaps make a newprivate-page
model ofpage
type with this setting switched off.
-
From src → app → landing-page ->. Open
landing-page.component.ts
and add or replace content with below codeimport { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; @Component({ selector: 'app-landing-page', templateUrl: './landing-page.component.html', styleUrls: ['./landing-page.component.css'] }) export class LandingPageComponent implements OnInit { noBuilderPageForUrl: boolean = false; builderRSP: Observable<any> | undefined; constructor(private http:HttpClient){ } // Replace the builder-api-key and private key from your own space. ngOnInit():void { let apiUrl = 'https://cdn.builder.io/api/v2/content' let modelName = 'page' this.builderRSP = this.http.get<any>(`${apiUrl}/${modelName}?apiKey=builder-api-key&limit=1&userAttributes.urlPath=/`,{ headers: { Authorization: `Bearer bpk-private-key` }, }).pipe(map(data => data.results[0])) } }
Note: In case you get Error: Module not found: Error: Can’t resolve ‘@angular/elements’,
To resolve this install dependency for @angular/elements
using below command
npm i --save @angular/elements