Skip to content

Commit d89236e

Browse files
authored
docs(angular): Update README for more recent Angular versions (#14767)
Update the README.md to include examples with `ApplicationConfig` as a first-class citizen, as it is now the recommended approach. Most users are likely using the latest versions, but older examples have also been preserved.
1 parent d773cb7 commit d89236e

File tree

1 file changed

+40
-46
lines changed

1 file changed

+40
-46
lines changed

packages/angular/README.md

+40-46
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
## Angular Version Compatibility
1919

20-
This SDK officially supports Angular 15 to 17.
20+
This SDK officially supports Angular 14 to 19.
2121

2222
If you're using an older Angular version please check the
2323
[compatibility table in the docs](https://docs.sentry.io/platforms/javascript/guides/angular/#angular-version-compatibility).
@@ -33,35 +33,40 @@ in `@sentry/browser` can be imported from `@sentry/angular`.
3333
To use this SDK, call `Sentry.init(options)` before you bootstrap your Angular application.
3434

3535
```javascript
36-
import { enableProdMode } from '@angular/core';
37-
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
36+
import { bootstrapApplication } from '@angular/platform-browser';
3837
import { init } from '@sentry/angular';
3938

40-
import { AppModule } from './app/app.module';
39+
import { AppComponent } from './app/app.component';
4140

4241
init({
4342
dsn: '__DSN__',
4443
// ...
4544
});
4645

47-
// ...
48-
49-
enableProdMode();
50-
platformBrowserDynamic()
51-
.bootstrapModule(AppModule)
52-
.then(success => console.log(`Bootstrap success`))
53-
.catch(err => console.error(err));
46+
bootstrapApplication(AppComponent, appConfig);
5447
```
5548

5649
### ErrorHandler
5750

5851
`@sentry/angular` exports a function to instantiate an ErrorHandler provider that will automatically send Javascript
5952
errors captured by the Angular's error handler.
6053

61-
```javascript
62-
import { NgModule, ErrorHandler } from '@angular/core';
54+
```ts
55+
import { ApplicationConfig, NgModule, ErrorHandler } from '@angular/core';
6356
import { createErrorHandler } from '@sentry/angular';
6457

58+
export const appConfig: ApplicationConfig = {
59+
providers: [
60+
{
61+
provide: ErrorHandler,
62+
useValue: createErrorHandler({
63+
showDialog: true,
64+
}),
65+
},
66+
],
67+
};
68+
69+
// Or using an old module approach:
6570
@NgModule({
6671
// ...
6772
providers: [
@@ -104,42 +109,27 @@ init({
104109
});
105110
```
106111

107-
2. Register `SentryTrace` as a provider in Angular's DI system, with a `Router` as its dependency:
112+
2. Inject the `TraceService` in the `APP_INITIALIZER`:
108113

109-
```javascript
110-
import { NgModule } from '@angular/core';
111-
import { Router } from '@angular/router';
112-
import { TraceService } from '@sentry/angular';
114+
```ts
115+
import { ApplicationConfig, APP_INITIALIZER, provideAppInitializer } from '@angular/core';
113116

114-
@NgModule({
115-
// ...
117+
export const appConfig: ApplicationConfig = {
116118
providers: [
117119
{
118-
provide: TraceService,
119-
deps: [Router],
120+
provide: APP_INITIALIZER,
121+
useFactory: () => () => {},
122+
deps: [TraceService],
123+
multi: true,
120124
},
121-
],
122-
// ...
123-
})
124-
export class AppModule {}
125-
```
126-
127-
3. Either require the `TraceService` from inside `AppModule` or use `APP_INITIALIZER` to force-instantiate Tracing.
128-
129-
```javascript
130-
@NgModule({
131-
// ...
132-
})
133-
export class AppModule {
134-
constructor(trace: TraceService) {}
135-
}
136-
```
137-
138-
or
139125

140-
```javascript
141-
import { APP_INITIALIZER } from '@angular/core';
126+
// Starting with Angular 19, we can use `provideAppInitializer`
127+
// instead of directly providing `APP_INITIALIZER` (deprecated):
128+
provideAppInitializer(() => inject(TraceService)),
129+
],
130+
};
142131

132+
// Or using an old module approach:
143133
@NgModule({
144134
// ...
145135
providers: [
@@ -149,6 +139,10 @@ import { APP_INITIALIZER } from '@angular/core';
149139
deps: [TraceService],
150140
multi: true,
151141
},
142+
143+
// Starting with Angular 19, we can use `provideAppInitializer`
144+
// instead of directly providing `APP_INITIALIZER` (deprecated):
145+
provideAppInitializer(() => inject(TraceService)),
152146
],
153147
// ...
154148
})
@@ -161,15 +155,15 @@ To track Angular components as part of your transactions, you have 3 options.
161155

162156
_TraceDirective:_ used to track a duration between `OnInit` and `AfterViewInit` lifecycle hooks in template:
163157

164-
```javascript
158+
```ts
165159
import { TraceModule } from '@sentry/angular';
166160

167-
@NgModule({
168-
// ...
161+
@Component({
162+
selector: 'some-component',
169163
imports: [TraceModule],
170164
// ...
171165
})
172-
export class AppModule {}
166+
export class SomeComponentThatUsesTraceDirective {}
173167
```
174168

175169
Then, inside your component's template (keep in mind that the directive's name attribute is required):

0 commit comments

Comments
 (0)