17
17
18
18
## Angular Version Compatibility
19
19
20
- This SDK officially supports Angular 15 to 17 .
20
+ This SDK officially supports Angular 14 to 19 .
21
21
22
22
If you're using an older Angular version please check the
23
23
[ 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`.
33
33
To use this SDK, call ` Sentry.init(options) ` before you bootstrap your Angular application.
34
34
35
35
``` javascript
36
- import { enableProdMode } from ' @angular/core' ;
37
- import { platformBrowserDynamic } from ' @angular/platform-browser-dynamic' ;
36
+ import { bootstrapApplication } from ' @angular/platform-browser' ;
38
37
import { init } from ' @sentry/angular' ;
39
38
40
- import { AppModule } from ' ./app/app.module ' ;
39
+ import { AppComponent } from ' ./app/app.component ' ;
41
40
42
41
init ({
43
42
dsn: ' __DSN__' ,
44
43
// ...
45
44
});
46
45
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);
54
47
```
55
48
56
49
### ErrorHandler
57
50
58
51
` @sentry/angular ` exports a function to instantiate an ErrorHandler provider that will automatically send Javascript
59
52
errors captured by the Angular's error handler.
60
53
61
- ``` javascript
62
- import { NgModule , ErrorHandler } from ' @angular/core' ;
54
+ ``` ts
55
+ import { ApplicationConfig , NgModule , ErrorHandler } from ' @angular/core' ;
63
56
import { createErrorHandler } from ' @sentry/angular' ;
64
57
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:
65
70
@NgModule ({
66
71
// ...
67
72
providers: [
@@ -104,42 +109,27 @@ init({
104
109
});
105
110
```
106
111
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 ` :
108
113
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' ;
113
116
114
- @NgModule ({
115
- // ...
117
+ export const appConfig: ApplicationConfig = {
116
118
providers: [
117
119
{
118
- provide: TraceService,
119
- deps: [Router],
120
+ provide: APP_INITIALIZER ,
121
+ useFactory : () => () => {},
122
+ deps: [TraceService ],
123
+ multi: true ,
120
124
},
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
139
125
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
+ };
142
131
132
+ // Or using an old module approach:
143
133
@NgModule ({
144
134
// ...
145
135
providers: [
@@ -149,6 +139,10 @@ import { APP_INITIALIZER } from '@angular/core';
149
139
deps: [TraceService ],
150
140
multi: true ,
151
141
},
142
+
143
+ // Starting with Angular 19, we can use `provideAppInitializer`
144
+ // instead of directly providing `APP_INITIALIZER` (deprecated):
145
+ provideAppInitializer (() => inject (TraceService )),
152
146
],
153
147
// ...
154
148
})
@@ -161,15 +155,15 @@ To track Angular components as part of your transactions, you have 3 options.
161
155
162
156
_ TraceDirective:_ used to track a duration between ` OnInit ` and ` AfterViewInit ` lifecycle hooks in template:
163
157
164
- ``` javascript
158
+ ``` ts
165
159
import { TraceModule } from ' @sentry/angular' ;
166
160
167
- @NgModule ({
168
- // ...
161
+ @Component ({
162
+ selector: ' some-component ' ,
169
163
imports: [TraceModule ],
170
164
// ...
171
165
})
172
- export class AppModule {}
166
+ export class SomeComponentThatUsesTraceDirective {}
173
167
```
174
168
175
169
Then, inside your component's template (keep in mind that the directive's name attribute is required):
0 commit comments