admin管理员组文章数量:1302407
In an Astro component MyScript.astro
with a fetch
call i am requesting the html
from the response. It's returning e.g. <script async src="https://my-api"></script>
.
// MyScript.astro
---
const { prop1, prop2 } = Astro.props;
const response = await fetch(import.meta.env.API_URL, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
'My-Key': import.meta.env.API_KEY,
}),
body: JSON.stringify({
prop1,
prop2
}),
});
const { html } = await response.json();
---
How do I return / output html
in the body of the page as code?
Update:
I use now:
<Fragment set:html={html} />
That seems to work.
In an Astro component MyScript.astro
with a fetch
call i am requesting the html
from the response. It's returning e.g. <script async src="https://my-api"></script>
.
// MyScript.astro
---
const { prop1, prop2 } = Astro.props;
const response = await fetch(import.meta.env.API_URL, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
'My-Key': import.meta.env.API_KEY,
}),
body: JSON.stringify({
prop1,
prop2
}),
});
const { html } = await response.json();
---
How do I return / output html
in the body of the page as code?
Update:
I use now:
<Fragment set:html={html} />
That seems to work.
Share Improve this question edited Feb 10 at 17:09 meez asked Feb 10 at 15:02 meezmeez 4,79810 gold badges55 silver badges123 bronze badges1 Answer
Reset to default -2You can use set:html
:
// MyScript.astro
---
const data = await fetch(url).then(res => res.json())
---
<Fragment set:html={data.html} />
版权声明:本文标题:javascript - How to return `html` response from Astro component as code inside head or body tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741710052a2393790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论