What is the best practice for storing all app Strings in Ionic 3. Should I created one data provider and store all strings as constants?
Asked
Active
Viewed 986 times
2

Sampath
- 63,341
- 64
- 307
- 441

Tapas Mukherjee
- 2,088
- 1
- 27
- 66
-
Possible duplicate of [App.settings - the Angular-4 way?](https://stackoverflow.com/questions/43193049/app-settings-the-angular-4-way) – Suraj Rao Oct 04 '17 at 04:56
-
Please also take a look at **[this ionic-related answer](https://stackoverflow.com/questions/39576991/ionic2-angular2-read-a-custom-config-file/39577841#39577841)** – sebaferreras Oct 04 '17 at 05:48
-
1@sebaferreras OpaqueToken was deprecated for InjectionToken in Angular 4. I suggest updating that answer :) – Suraj Rao Oct 04 '17 at 05:51
-
Thanks for pointing that out @suraj. Seems like I had two different answers for the same question... I've updated the link to the comment and retracted my close vote since it was the wrong answer. – sebaferreras Oct 04 '17 at 05:54
1 Answers
0
There are multiple ways.But I have chosen very simple approach here.That is, Just create a static class
as shown below.
Note: I extracted below example from my working project.
Constant file:
Handlers.ts
export class Handlers {
static BUDGET_PAGE_TOTAL_HANDLER = "budget-page-total-handler";
static NEW_PROJECT_PAGE_BUDGET_HANDLER = "new-project-page-budget-handler";
static HOME_PAGE_TRANSACTION_HANDLER = "home-page-transaction-handler";
}
Use like this:
.ts
import { Handlers } from '../../constants/Handlers';
eventHandlers() {
this.events.subscribe(Handlers.HOME_PAGE_TRANSACTION_HANDLER, this.transactionHandler);
}

Sampath
- 63,341
- 64
- 307
- 441
-
Interesting.. Although Angular way is to use [injectiontoken](https://stackoverflow.com/questions/43193049/app-settings-the-angular-4-way) and inject in constructor wherever necessary – Suraj Rao Oct 04 '17 at 05:08
-
Yeah.. There are multiple ways. I have chosen very simple approach here and working fine :) @suraj – Sampath Oct 04 '17 at 05:47
-
1The only difference is that **injecting** the _instance of the class_ (instead of using the static properties from the class directly) will allow you to write unit tests in an easier way (you can easily mock everything). But they should both work properly – sebaferreras Oct 04 '17 at 05:51
-
Thank you. Also is there any issue if I use constants without injecting in the constructor? – Tapas Mukherjee Oct 04 '17 at 14:45
-
There is no problem if you use constants while just importing them. I personally think its much more clearer to read than static properties of a class – misha130 Oct 04 '17 at 16:05
-
1Any reason why you aren't declaring ngx-translate content in your project. Then using the Translation Service/Translate pipe to load the content? https://ionicframework.com/docs/developer-resources/ng2-translate/ – Ernesto Rendon Oct 04 '17 at 18:04
-
You can see the difference if you read the `sebaferreras` comment above. – Sampath Oct 04 '17 at 21:47
-