如何将 API 数据设置为内部 html with lit
how can I set API data as inner html with lit
我有 html 个点亮的元素
我要设置它们的内部 html
获取请求
如何设置?
import { LitElement, html, css } from "lit";
import { customElement } from "lit/decorators.js";
import axios from "axios";
@customElement("s-profile")
export class Profile extends LitElement {
render() {
return html`<p>${getProfile()}</p>`;
}
}
// get data from api
async function getProfile(): Promise<string> {
const username = window.location.pathname.replace("/", "");
const result = await axios.get(
`http://localhost:8000/api/getProfile?username=${username}`
);
const data: string = (<any>result).data.result.username;
console.log(data);
return data;
}
你可以使用updateRequest()
import { LitElement, html, css } from "lit";
import { customElement, property } from "lit/decorators.js";
import axios from "axios";
@customElement("s-profile")
export class Profile extends LitElement {
@property({ type: Object })
data = {};
// get user data from api
async getProfile() {
const username = window.location.pathname.replace("/", "");
const result = await axios.get(
`http://localhost:8000/api/getProfile?username=${username}`
);
const data: UserData = (<any>result).data.result;
this.data = data;
this.requestUpdate();
}
connectedCallback() {
super.connectedCallback();
this.getProfile();
}
render() {
return html`<p>${this.data}</p>`;
}
}
我有 html 个点亮的元素
我要设置它们的内部 html 获取请求
如何设置?
import { LitElement, html, css } from "lit";
import { customElement } from "lit/decorators.js";
import axios from "axios";
@customElement("s-profile")
export class Profile extends LitElement {
render() {
return html`<p>${getProfile()}</p>`;
}
}
// get data from api
async function getProfile(): Promise<string> {
const username = window.location.pathname.replace("/", "");
const result = await axios.get(
`http://localhost:8000/api/getProfile?username=${username}`
);
const data: string = (<any>result).data.result.username;
console.log(data);
return data;
}
你可以使用updateRequest()
import { LitElement, html, css } from "lit";
import { customElement, property } from "lit/decorators.js";
import axios from "axios";
@customElement("s-profile")
export class Profile extends LitElement {
@property({ type: Object })
data = {};
// get user data from api
async getProfile() {
const username = window.location.pathname.replace("/", "");
const result = await axios.get(
`http://localhost:8000/api/getProfile?username=${username}`
);
const data: UserData = (<any>result).data.result;
this.data = data;
this.requestUpdate();
}
connectedCallback() {
super.connectedCallback();
this.getProfile();
}
render() {
return html`<p>${this.data}</p>`;
}
}