2021-11-04 11:27:18 +00:00
# Angular
2021-11-12 15:31:13 +00:00
Angular [plays nice ](https://custom-elements-everywhere.com/#angular ) with custom elements, so you can use Shoelace in your Angular apps with ease.
2021-11-04 11:27:18 +00:00
## Installation
2021-11-12 15:31:13 +00:00
To add Shoelace to your Angular app, install the package from npm.
```bash
npm install @shoelace -style/shoelace
```
2023-05-24 14:34:05 +00:00
Next, [include a theme ](/getting-started/themes ) into your project. In this example, we'll import the light theme and use the CDN as a base path.
Include the theme into your `angular.json` config.
```json
...
"styles": [
"src/app/theme/styles.scss",
"@shoelace-style/shoelace/dist/themes/light.css"
],
...
```
OR
include it into your `styles.scss` .
```scss
@use "@shoelace-style/shoelace/dist/themes/light.css";
...
```
After that set the [base path ](/getting-started/installation#setting-the-base-path ) in your `AppModule` for icons and other assets.
2021-11-12 15:31:13 +00:00
```jsx
import { setBasePath } from '@shoelace-style/shoelace/dist/utilities/base-path';
setBasePath('https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@%VERSION%/dist/');
```
?> If you'd rather not use the CDN for assets, you can create a build task that copies `node_modules/@shoelace-style/shoelace/dist/assets` into a public folder in your app. Then you can point the base path to that folder instead.
## Configuration
Then make sure to apply the custom elements schema as shown below.
2021-11-04 11:27:18 +00:00
```js
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { AppComponent } from './app.component';
2023-05-24 14:34:05 +00:00
import { setBasePath } from '@shoelace-style/shoelace/dist/utilities/base-path';
setBasePath('https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@%VERSION%/dist/');
2021-11-04 11:27:18 +00:00
@NgModule ({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {}
```
2023-03-22 20:39:20 +00:00
## Reference Shoelace components in your Angular component code
```js
import { SlDrawer } from '@shoelace-style/shoelace';
@Component ({
selector: 'app-drawer-example',
2023-05-24 14:34:05 +00:00
template: '< div id = "page" >< button ( click )=" showDrawer ()" > Show drawer</ button >< sl-drawer #drawer label = "Drawer" class = "drawer-focus" style = "--size: 50vw" >< p > Drawer content</ p ></ sl-drawer ></ div > ',
providers: [SlDrawer]
2023-03-22 20:39:20 +00:00
})
export class DrawerExampleComponent implements OnInit {
// use @ViewChild to get a reference to the #drawer element within component template
@ViewChild ('drawer')
drawer?: ElementRef< SlDrawer > ;
...
constructor(...) {
}
2023-03-23 12:39:20 +00:00
2023-03-22 20:39:20 +00:00
ngOnInit() {
}
...
2023-03-23 12:39:20 +00:00
2023-03-22 20:39:20 +00:00
showDrawer() {
// use nativeElement to access Shoelace components
this.drawer?.nativeElement.show();
}
}
```
2021-11-12 15:31:13 +00:00
Now you can start using Shoelace components in your app!
?> Are you using Shoelace with Angular? [Help us improve this page! ](https://github.com/shoelace-style/shoelace/blob/next/docs/frameworks/angular.md )