Files
webawesome/docs/pages/frameworks/angular.md

113 lines
3.0 KiB
Markdown
Raw Normal View History

2023-06-06 08:22:18 -04:00
---
meta:
title: Angular
2023-09-08 13:45:49 -04:00
description: Tips for using Web Awesome in your Angular app.
2023-06-06 08:22:18 -04:00
---
# Angular
2023-09-08 13:45:49 -04:00
Angular [plays nice](https://custom-elements-everywhere.com/#angular) with custom elements, so you can use Web Awesome in your Angular apps with ease.
2023-06-06 08:22:18 -04:00
## Installation
2023-10-20 09:04:15 -04:00
### Download the npm package
2023-09-08 13:45:49 -04:00
To add Web Awesome to your Angular app, install the package from npm.
2023-06-06 08:22:18 -04:00
```bash
npm install @shoelace-style/shoelace
```
2023-10-20 09:04:15 -04:00
### Update the Angular Configuration
Next, [include a theme](/getting-started/themes). In this example, we'll import the light theme.
2023-10-23 10:19:34 -04:00
Its also important to load the components by using a `<script>` tag into the index.html file. However, the Angular way to do it is by adding a script configurations into your angular.json file as follows:
2023-10-20 09:04:15 -04:00
2023-10-23 10:48:37 -04:00
```json
2023-10-20 09:04:15 -04:00
"architect": {
"build": {
...
"options": {
...
"styles": [
"src/styles.scss",
"@shoelace-style/shoelace/dist/themes/light.css"
],
"scripts": [
"@shoelace-style/shoelace/dist/shoelace.js"
]
...
```
### Setting up the base path
Next, set the [base path](/getting-started/installation#setting-the-base-path) for icons and other assets in the `main.ts`. In this example, we'll use the CDN as a base path.
2023-06-06 08:22:18 -04:00
```jsx
2023-06-12 13:45:27 -04:00
import { setBasePath } from '@shoelace-style/shoelace/%NPMDIR%/utilities/base-path';
2023-06-06 08:22:18 -04:00
2023-06-12 13:45:27 -04:00
setBasePath('https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@%VERSION%/%CDNDIR%/');
2023-06-06 08:22:18 -04:00
```
:::tip
2023-06-12 13:45:27 -04:00
If you'd rather not use the CDN for assets, you can create a build task that copies `node_modules/@shoelace-style/shoelace/%NPMDIR%/assets` into a public folder in your app. Then you can point the base path to that folder instead.
2023-06-06 08:22:18 -04:00
:::
## Configuration
Then make sure to apply the custom elements schema as shown below.
```js
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {}
```
2023-09-08 13:45:49 -04:00
## Reference Web Awesome components in your Angular component code
2023-06-06 08:22:18 -04:00
```js
2023-09-08 13:45:49 -04:00
import { WaDrawer } from '@shoelace-style/shoelace';
2023-06-06 08:22:18 -04:00
@Component({
selector: 'app-drawer-example',
2023-09-08 13:45:49 -04:00
template: '<div id="page"><button (click)="showDrawer()">Show drawer</button><wa-drawer #drawer label="Drawer" class="drawer-focus" style="--size: 50vw"><p>Drawer content</p></wa-drawer></div>'
2023-06-06 08:22:18 -04:00
})
export class DrawerExampleComponent implements OnInit {
// use @ViewChild to get a reference to the #drawer element within component template
@ViewChild('drawer')
2023-09-08 13:45:49 -04:00
drawer?: ElementRef<WaDrawer>;
2023-06-06 08:22:18 -04:00
...
constructor(...) {
}
ngOnInit() {
}
...
showDrawer() {
2023-09-08 13:45:49 -04:00
// use nativeElement to access Web Awesome components
2023-06-06 08:22:18 -04:00
this.drawer?.nativeElement.show();
}
}
```
2023-09-08 13:45:49 -04:00
Now you can start using Web Awesome components in your app!
2023-06-06 08:22:18 -04:00
:::tip
2023-09-08 13:45:49 -04:00
Are you using Web Awesome with Angular? [Help us improve this page!](https://github.com/shoelace-style/shoelace/blob/next/docs/frameworks/angular.md)
2023-06-12 14:20:11 -04:00
:::