First you need to install project dependencies
npm install or yarn install
you can use angular cli command ng generante environments or if you want to create your files manually, just add environment.ts and environment.development.ts in ./src/environments folder
you can find a configuration example
export const environment = {
production: true,
PAGESPEED_API_KEY: "your_api_key",
PAGESPEED_API_ROOT: "https://www.googleapis.com/pagespeedonline/v5/runPagespeed",
API_ROOT: "http://localhost:3000",
SECURE_URL_CODE: "660",
};Run npm run dev to start both development server and mock api server
The configuration is in json-server.json in the root folder
{
"delay": 1000,
"middlewares":[
"./node_modules/json-server-auth"
],
"watch": true
}
Run npm run start for a dev server. Navigate to http://localhost:4200/. The application will automatically reload if you change any of the source files.
Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.
Run npm run build to build the project. The build artifacts will be stored in the dist/ directory.
Run npm run test to execute the unit tests via Karma.
This class can be used as a base for each class in the application
A variable that contains shortcuts vor some bundle of classes
{
container: "max-w-6xl mx-auto p-8",
fullBlock: "block w-full",
}
<div [class]="classUtils.container"></div>it creates a new subscription that will be unsuscribed on OnDestroy
@Component({
...
})
export class Component implements EssentialComponent implements OnInit{
obs$: Observable<any>
...
ngOnInit(){
this.subscription.add(
obs$.subscribe(...)
)
}
}You can use this class for each call to the backend server
@Injectable()
export class UserService implements FetchDataService {
constructor(http: HttpClient, feedback: FeedbackService, loading: LoadingService) {
super(http, feedback, loading);
}
public getUsers(): Observable<User[]> {
return this.fetch<User[]>({
url: `${this.apiRoot}/sites`,
method: "GET",
params: this.buildParams({
_embed: "audits",
}),
});
}
}| param | required | type |
|---|---|---|
| url | required | string |
| method | required | 'GET' 'POST' 'PUT' 'DELETE' |
| body | optional | {} |
| headers | optional | HttpHeaders |
| params | optional | HttpParams |
this method is used to update localLoading$ observable that emits a boolean used to implement a loading indicator for each service that implements the class
In service
@Injectable()
export class UserService implements FetchDataService{
constructor(
...
) {
...
}
public getUsers(): Observable<User[]> {
const res$ = this.fetch<User[]>({
url: `${this.apiRoot}/sites`,
method: 'GET',
params: this.buildParams({
_embed: 'audits'
}),
})
return this.handleLocalLoading(res$);
}
}In component
@Component({
selector: "app-user",
template: ` <div class="user-box" *ngIf="loading$ | async"></div> `,
})
export class AppComponent {
loading$ = this.userService.localLoading$;
constructor(private userService: UserService);
}this variable is used to add 660 to url, useful in development to simulate permissions on api endpoints
export class UserService implements FetchDataService{
constructor(
...
) {
...
}
public getUser(id: number): Observable<User> {
return this.fetch<User>({
url: `${this.secureUrlCode}/users/${id}`,
method: 'GET',
})
}
}To get more help on the Angular CLI use ng help or go check out the Angular CLI Overview and Command Reference page.