Skip to content

Instantly share code, notes, and snippets.

@touhidrahman
Created January 25, 2022 11:56
Show Gist options
  • Save touhidrahman/f3d7719fb91854e5a770cb804d599b1f to your computer and use it in GitHub Desktop.
Save touhidrahman/f3d7719fb91854e5a770cb804d599b1f to your computer and use it in GitHub Desktop.
Sanity Usage in Component
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