Created
June 7, 2019 18:19
-
-
Save Tallyb/1c7ecf19d3ab3e1ffde31c27594acd6f to your computer and use it in GitHub Desktop.
Fetch 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
import { Component, h, JSX, Prop, Watch } from '@stencil/core'; | |
@Component({ | |
tag: 'my-fetch', | |
styleUrl: 'fetch.css' | |
}) | |
export class MyFetchComponent { | |
@Prop() language: string; | |
@Watch('language') | |
async onLanguageChange() { | |
if (this.language) { | |
return fetch('./assets/i18n/' + this.language + '.json') | |
.then(response => { | |
if (!response.ok) { | |
throw new Error('HTTP error ' + response.status); | |
} | |
return response.json(); | |
}) | |
.then(json => { | |
console.log('JSON', json); | |
}); | |
} | |
} | |
componentWillLoad() { | |
if (this.language) { | |
this.onLanguageChange(); | |
} | |
} | |
render(): JSX.Element { | |
return <slot></slot>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment