forked from LarsDenBakker/lit-html-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-child-component.html
75 lines (64 loc) · 1.61 KB
/
4-child-component.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!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();
this.articles = [];
}
connectedCallback() {
super.connectedCallback();
fetch('https://newsapi.org/v2/everything?q=tech&apiKey=<your-api-key>')
.then(response => response.json())
.then(response => {
this.articles = response.articles;
});
}
render() {
return html`
<ul>
${this.articles.map(article => html`
<!-- use your new element -->
<my-article
.title=${article.title}
.description=${article.description}
></my-article>
`)}
</ul>
`;
}
}
customElements.define('my-element', MyElement);
// define a new element
class MyArticle extends LitElement {
static get properties() {
return {
title: { type: String },
description: { type: String }
}
}
render() {
return html`
<li>
<h2>${this.title}</h2>
<p>${this.description}</p>
</li>
`;
}
}
customElements.define('my-article', MyArticle);
</script>
</head>
<body>
<my-element></my-element>
</body>
</html>