Angular 5 存储开发者个人设置的最佳实践及架构范例咨询
Great question! This is a super common scenario in team-based development, and there are some tried-and-true best practices to handle it cleanly:
The key is to separate shared, non-sensitive configuration (which belongs in the repo) from personal/local/sensitive configuration (which stays on individual machines). Here's how to implement it:
1. Keep Public Config Files in the Repo
Your existing config.dev.json and config.prod.json are perfect for shared settings like:
- Base API endpoints
- Feature flags that apply to all environments
- Default UI preferences
Commit these files to Git so every team member has the same starting point.
2. Create a Template for Local/Personal Config
Make a config.local.example.json file in the same src/assets/config directory. This template should outline all the personal/sensitive fields that need local customization, with comments explaining their purpose. For example:
{ // Your unique user ID for local testing "userId": "YOUR_PERSONAL_USER_ID", // Local-only API keys (never commit real values!) "localApiKey": "YOUR_LOCAL_API_KEY" }
Commit this template to the repo so new team members know exactly what they need to configure locally.
3. Ignore the Actual Local Config File
Have each developer copy the template to a new file named config.local.json (this is the file they'll edit with their real personal values). Then add this line to your .gitignore to ensure Git never tracks it:
src/assets/config/config.local.json
4. Merge Configs in Your Code
In your application, load the public config first, then overlay the local config (if it exists) so personal settings take precedence. Here's a quick JavaScript example:
import baseConfig from './config.dev.json'; // Load local config (fail silently if it doesn't exist) let localConfig = {}; try { localConfig = require('./config.local.json'); } catch (error) { // No local config? No problem—stick with base settings } // Merge configs: local values override base values const finalConfig = { ...baseConfig, ...localConfig };
- Never commit sensitive data: Double-check that your public config files don't contain API keys, user IDs, or any other data that shouldn't be shared publicly.
- Use environment variables for cross-environment switches: For example, in a Node.js or frontend project, use
process.env.NODE_ENVto automatically loadconfig.dev.jsonorconfig.prod.json, then merge withconfig.local.json. - Document the workflow: Add a note in your
README.mdexplaining how to set up local config (copy the example file, fill in personal values) so new team members don't get stuck.
This "base config + local override" pattern is widely used across many frameworks and projects:
- Frontend frameworks like Vue CLI and Create React App use this logic with
.env(public) and.env.local(ignored) files for environment variables. - Node.js projects often use the
confignpm package, which supports loading environment-specific config files and automatically picks uplocal-*.jsonfiles (which are typically ignored via.gitignore). - Backend frameworks like Django use
settings.py(public) alongsidelocal_settings.py(ignored) for the same purpose.
内容的提问来源于stack exchange,提问作者L Strand

