How to clear console information when packaging and publishing in angular2+

1. Why should we clear the console information?

As a developer, it is inevitable to log information to debug the program. Sometimes we may need to log more than ten logs for a module, and each log is identified by a different string. Since the log logged each time may be debugged next time It's still useful, so we won't delete it. (ps: I used to think that clearing the log wouldn't be so troublesome? But when I encounter a new bug, I have to delete the console last time. In addition, after doing this many times, I decided not to clear the console.) However, if the product you package and release also has console information, it always feels a bit weird. If there are still errors reported in the console, then . . .

2. Use angular-cli to distinguish dev mode and prod mode

Using angular-cli, we can easily know whether the current code environment is in dev mode or prod mode. In the development environment of angular-cli scaffolding, there is an environments folder, which contains an environment.ts file, and there is an object in it for Identifies whether it is dev mode or prod mode.
The environments folder in the src directory of the angular-cli development environment:

Environment.ts file content:

The above comment probably means: when you execute ng build --env=prodthe command to package the project, the content of environment.prod.ts will replace the content of environment.ts. For details about who replaces whom, we can go to angular-cli.json Configure inside

angular-cli.json file configuration:

3. How to clear the console in prod mode

Since main.ts is the starting point for the angular program, we only need to determine whether it is dev mode or prod mode in this file. If it is prod mode, we will rewrite the console, and then the code in prod mode will not print console information. , the specific implementation is as follows:

Guess you like

Origin blog.csdn.net/luo1055120207/article/details/76064115