Angular 2, sprintf in TypeScript

Can someone tell me all the steps that I need to take to use the full sprintf library (with zero padding and all other functions that are usually present in standard sprintf) in the Angular 2 Typescript component? Thanks.

+5
source share
5 answers

Install sprintf-js

npm install sprintf-js --save 

Install TypeScript Definition

 typings install dt~sprintf-js --global --save 

Import and use in code

 import {sprintf} from "sprintf-js"; console.log(sprintf("%s %s!", "Hello", "Massimo")); 
+14
source

npm i sprintf-js --save

npm i --save_dev @types/sprintf-js

import {sprintf} from "sprintf-js";

+8
source

Since the vise is now outdated:

$ npm install --save @types/sprintf

Then in the typescript source:

import { sprintf } from 'sprintf';

+5
source

I answer my question.

Download this library: https://github.com/alexei/sprintf.js

Add a link to the sprintf.js file inside the main tag of the main index.html file:

 <script src="assets/js/sprintf.js"></script> 

Then, in the component, declare the main sprintf function as follows, to use as usual anyway.

 declare var sprintf: any; console.log(sprintf("%s %s!", "Hello", "Massimo")); // output : Hello Massimo! 
0
source

Using ionic2.

None of the above solutions helped me.

However, after launch:

 npm install sprintf-js --save npm install --save @types/sprintf 

I managed to use require('sprintf-js') in my app.ts file.

After that, vsprintf and sprintf worked while I added

 declare var vsprintf:any; declare var sprintf:any; 

at the top of the ts file that used them.

0
source

Source: https://habr.com/ru/post/1012521/


All Articles