Created
January 25, 2022 11:56
-
-
Save touhidrahman/f3d7719fb91854e5a770cb804d599b1f to your computer and use it in GitHub Desktop.
Sanity Usage in Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Movie { | |
_id: String, | |
title: String, | |
releaseDate: Date, | |
poster: SanityImageSource, // = string | SanityReference | SanityAsset | SanityImageObject etc. | |
} | |
@Component({ | |
selector: 'app-movies', | |
template: ` | |
<main class="container"> | |
<div *ngFor="let movie of movies$ | async"> | |
<img [src]="movie.poster | sanityImage:200" /> | |
<h3>{{ movie.title }}</h3> | |
<p>Released on {{ movie.releaseDate | date }}</p> | |
</div> | |
</main> | |
`, | |
styleUrls: ['./movies.component.css'] | |
}) | |
export class MoviesComponent implements OnInit { | |
movies$: Observable<Movie[]> | |
constructor(private sanityService: SanityService ) { | |
this.movies$ = this.sanityService.fetch<Movie[]>( | |
`*[_type == "movie"]{ | |
_id, | |
title, | |
releaseDate, | |
poster | |
}` | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment