Frequently Asked Questions
This document汇总了常见的技术问题和解答。covers common technical questions and answers that come up when working with Angular.
Getting Started
What is Angular?
Angular is an open-source web application framework developed and maintained by Google for building modern web applications. It uses TypeScript as its primary language and provides features like component-based architecture, two-way data binding, and dependency injection.
What's the difference between Angular and AngularJS?
AngularJS is the name for Angular 1.x—the original version. Angular is a complete rewrite with a totally different architecture using TypeScript. They're not compatible with each other.
Is Angular hard to learn?
Angular has a learning curve, especially for concepts like dependency injection and RxJS that take time to understand. But the documentation is excellent, the community is active, and once you get the hang of it, development is very efficient.
Installation Issues
npm install keeps failing. What should I do?
- Clear npm cache:
npm cache clean --force - Delete node_modules and package-lock.json
- Use a Chinese mirror
- Check if your Node.js version is compatible
Angular CLI command not found?
Make sure it's installed globally:
npm install -g @angular/cli
If that doesn't work, try using npx:
npx @angular/cli new my-app
Node.js version incompatible?
Use nvm to manage Node.js versions:
nvm install 18
nvm use 18
Development Issues
Component styles not working?
- Check if the style file path is correct
- Make sure there are no syntax errors in styles.scss
- Check the ViewEncapsulation setting
Route navigation failing?
- Check if routes are configured correctly
- Confirm RouterModule is imported properly
- Check if a route guard is blocking navigation
HTTP requests failing?
- Check if the backend service is running
- Confirm CORS is configured
- Use HttpInterceptor to add error handling
Data not updating?
- Check your change detection strategy
- If using OnPush, you need to manually trigger detection
- Make sure the data reference has changed
Performance Issues
Slow initial load?
- Enable route lazy loading
- Use preloading strategies
- Optimize image resources
- Enable production mode for builds
Memory leaks?
- Unsubscribe from Observables in ngOnDestroy
- Use takeUntil or the async pipe
- Clean up timers and event listeners
Too many change detection runs?
- Use OnPush strategy
- Separate mutable from immutable properties
- Use trackBy to optimize *ngFor
Testing Issues
Unit tests failing?
- Check TestBed configuration
- Make sure async operations are handled properly
- Use fakeAsync and tick for testing async code
Module not found?
Make sure paths are configured correctly in tsconfig.json.
Build Issues
Build failing?
- Check the actual error message
- Review TypeScript compilation options
- Clear cache and rebuild
Build files too large?
- Enable production mode
- Use lazy loading
- Check for unused dependencies
Common Errors
ExpressionChangedAfterItHasBeenCheckedError
You modified a value during change detection that shouldn't change. Check if you're directly modifying view-related code in ngOnInit.
NG0100: ExpressionChangedAfterItHasBeenCheckedError
Same as above—check your data binding timing.
Cannot read property of undefined
You're accessing data before it's initialized. Use optional chaining (?.) or *ngIf to protect against this.
CORS errors
The backend hasn't configured CORS, or there's an issue with frontend request headers.
401/403 errors
Authentication failed. Check your token and permission configuration.
Version Upgrades
How do I upgrade Angular?
- Use CLI to upgrade:
ng update @angular/core - Check the official upgrade guide
- Upgrade dependencies one at a time
- Run tests to make sure everything works
Upgrade failed?
- Back up your code
- Manually update versions in package.json
- Delete node_modules and reinstall
Getting More Help
- Official docs: https://angular.io/docs
- GitHub: https://github.com/angular/angular
- Community: https://angular.io/discussions
