Skip to content

Commit e1daaff

Browse files
committed
platform browser check
1 parent ae2241c commit e1daaff

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This project was generated with [Angular CLI](https://github.com/angular/angular
2020
- Named and multiple router outlets
2121
- Migrate to Angular 11
2222
- Migrate tslint to esLint
23+
- Migrate to Angular 12
2324

2425
## Unit Tests
2526

angular.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"main": "src/main.ts",
3232
"polyfills": "src/polyfills.ts",
3333
"tsConfig": "tsconfig.app.json",
34+
"aot": true,
3435
"assets": [
3536
"src/favicon.ico",
3637
"src/assets",
@@ -41,13 +42,7 @@
4142
"./node_modules/ngx-toastr/toastr.css",
4243
"src/styles.scss"
4344
],
44-
"scripts": [],
45-
"vendorChunk": false,
46-
"extractLicenses": true,
47-
"buildOptimizer": true,
48-
"sourceMap": true,
49-
"optimization": true,
50-
"namedChunks": true
45+
"scripts": []
5146
},
5247
"configurations": {
5348
"production": {
@@ -63,7 +58,7 @@
6358
"extractLicenses": true,
6459
"vendorChunk": false,
6560
"buildOptimizer": true,
66-
"localize": true,
61+
"localize": false,
6762
"budgets": [
6863
{
6964
"type": "initial",

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function app() {
8080
}, (err, html) => {
8181
if (err) {
8282
console.error(err);
83-
res.send('An error occured: ' + err.message);
83+
res.send('An error occurred: ' + err.message);
8484
}
8585
return res.send(html);
8686
});

src/app/app.module.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {BrowserModule, BrowserTransferStateModule} from '@angular/platform-browser';
2-
import { NgModule } from '@angular/core';
2+
import {Inject, NgModule, PLATFORM_ID} from '@angular/core';
33

44
import { AppComponent } from './app.component';
55
import { AppRoutingModule } from './app-routing.module';
@@ -16,6 +16,7 @@ import {TranslateResources} from './translate-resources';
1616
import {StrengthPipe} from './pipes/strength/strength.pipe';
1717
import {ToastrModule} from 'ngx-toastr';
1818
import {ApiUrlInterceptor} from './interceptors/api-url.interceptor';
19+
import {isPlatformBrowser} from '@angular/common';
1920

2021
// AoT requires an exported function for factories
2122
export function HttpLoaderFactory(http: HttpClient) {
@@ -56,8 +57,15 @@ export function HttpLoaderFactory(http: HttpClient) {
5657
})
5758
export class AppModule {
5859

59-
constructor(private _translateService: TranslateService) {
60-
const lang = localStorage.getItem('lang') || 'en';
60+
constructor(@Inject(PLATFORM_ID) protected platformId: object,
61+
private _translateService: TranslateService) {
62+
63+
let lang = 'en';
64+
65+
if (isPlatformBrowser(this.platformId)) {
66+
lang = localStorage.getItem('lang') || 'en';
67+
}
68+
6169
this._translateService.addLangs(['en', 'fr']);
6270
this._translateService.setDefaultLang('en');
6371
this._translateService.use(lang);

src/app/modules/header/header.component.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import {Component, Inject, OnInit, PLATFORM_ID} from '@angular/core';
22
import {TranslateService} from '@ngx-translate/core';
3+
import {isPlatformBrowser} from '@angular/common';
34

45
@Component({
56
selector: 'app-header',
@@ -14,16 +15,20 @@ export class HeaderComponent implements OnInit {
1415

1516
private _selectedLang = this._translateService.currentLang || 'en';
1617

17-
constructor(private _translateService: TranslateService) { }
18+
constructor(@Inject(PLATFORM_ID) protected platformId: object,
19+
private _translateService: TranslateService) { }
1820

1921
ngOnInit(): void {
2022
}
2123

2224
public changeLang(event: Event, lang: string) {
2325
event.preventDefault();
24-
this._selectedLang = lang;
25-
localStorage.setItem('lang', this._selectedLang);
26-
this._translateService.use(this._selectedLang);
26+
27+
if (isPlatformBrowser(this.platformId)) {
28+
this._selectedLang = lang;
29+
localStorage.setItem('lang', this._selectedLang);
30+
this._translateService.use(this._selectedLang);
31+
}
2732
}
2833

2934
}

0 commit comments

Comments
 (0)