forked from LarsDenBakker/lit-html-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-render-articles.html
53 lines (44 loc) · 1.14 KB
/
3-render-articles.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type="module">
import {LitElement, html, css} from 'https://unpkg.com/[email protected]?module';
class MyElement extends LitElement {
static get properties() {
return {
articles: { type: Array }
};
}
constructor() {
super();
// by default, there are no articles
this.articles = [];
}
connectedCallback() {
super.connectedCallback();
fetch('https://newsapi.org/v2/everything/<your-api-key-goes-here>')
.then(response => response.json())
.then(response => {
this.articles = response.articles;
})
}
render() {
return html`
<ul>
<!-- use a map function to repeat a template -->
${this.articles.map(article => html`
<li>${article.title}</li>
`)}
</ul>
`;
}
}
customElements.define('my-element', MyElement);
</script>
</head>
<body>
<my-element></my-element>
</body>
</html>