在根目錄下tsconfig.json中添加baseUrl和pathsgit
{ "compilerOptions": { "baseUrl": "src", "paths": { "@app/*": ["app/*"], "@assets/*": ["assets/*"], "@env/*": ["environments/*"], "@views/*": ["app/views/*"], "@core/*": ["app/core/*"], "@constants": ["app/core/constants/*"], "@model/*": ["app/core/model/*"], "@interceptors/*": ["app/core/interceptors/*"], "@services/*": ["app/core/services/*"], "@validators/*": ["app/core/validators/*"], "@shared/*": ["app/shared/*"], "@components/*": ["app/shared/components/*"], "@directives/*": ["app/shared/directives/*"], "@guards/*": ["app/shared/guards/*"], "@pipes/*": ["app/shared/pipes/*"] }, }
import { environment } from '@env/environment'
添加了@env別名,以便可以import { environment } from '@env/environment',從應用程序中的任何位置輕鬆訪問環境變量。它適用於全部指定的環境,由於cli它會根據--env傳遞給ng build命令的標誌自動解析正確的環境文件。github