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?

  1. Clear npm cache: npm cache clean --force
  2. Delete node_modules and package-lock.json
  3. Use a Chinese mirror
  4. 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?

  1. Check if the style file path is correct
  2. Make sure there are no syntax errors in styles.scss
  3. Check the ViewEncapsulation setting

Route navigation failing?

  1. Check if routes are configured correctly
  2. Confirm RouterModule is imported properly
  3. Check if a route guard is blocking navigation

HTTP requests failing?

  1. Check if the backend service is running
  2. Confirm CORS is configured
  3. Use HttpInterceptor to add error handling

Data not updating?

  1. Check your change detection strategy
  2. If using OnPush, you need to manually trigger detection
  3. Make sure the data reference has changed

Performance Issues

Slow initial load?

  1. Enable route lazy loading
  2. Use preloading strategies
  3. Optimize image resources
  4. Enable production mode for builds

Memory leaks?

  1. Unsubscribe from Observables in ngOnDestroy
  2. Use takeUntil or the async pipe
  3. Clean up timers and event listeners

Too many change detection runs?

  1. Use OnPush strategy
  2. Separate mutable from immutable properties
  3. Use trackBy to optimize *ngFor

Testing Issues

Unit tests failing?

  1. Check TestBed configuration
  2. Make sure async operations are handled properly
  3. Use fakeAsync and tick for testing async code

Module not found?

Make sure paths are configured correctly in tsconfig.json.

Build Issues

Build failing?

  1. Check the actual error message
  2. Review TypeScript compilation options
  3. Clear cache and rebuild

Build files too large?

  1. Enable production mode
  2. Use lazy loading
  3. 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?

  1. Use CLI to upgrade: ng update @angular/core
  2. Check the official upgrade guide
  3. Upgrade dependencies one at a time
  4. Run tests to make sure everything works

Upgrade failed?

  1. Back up your code
  2. Manually update versions in package.json
  3. Delete node_modules and reinstall

Getting More Help