Tho*_*ggi 14 javascript astrojs
我希望能够在 Astro 中拥有一个动态页面来呈现 Astro 组件。我深入研究了文档和代码,但找不到像下面这样的函数(Astro.render)。理想情况下,我可以将属性传递给它。我正在寻找类似的东西react.renderToString。
import Example from './components/Example.astro'
export async function get() {
return {
body: Astro.render(Example)
};
}
Run Code Online (Sandbox Code Playgroud)
更新:我认为这里的每个答案都是错误的。给定一个 astro 文件,我想要一个node独立于运行时 astro 框架的函数,它可以接受 astro 文件并简单地返回一个Response(因为我知道 astro 文件可以返回来自 front-matter 的响应)和/或HTML字符串?我认为它可以是像这样的元组[res, htmlString]。就像可以转换 Markdown 文件一样,astro 文件也应该能够被处理。
was*_*ila 10
Astro 中确实存在渲染为 html 字符串的功能,但是对于插槽,如果将组件传递到另一个 Wrapper/Serialiser 中,则可以轻松获取其 html 字符串
github 仓库https://github.com/MicroWebStacks/astro-examples#16_html-string
CodeSandbox https://stackblitz.com/github/MicroWebStacks/astro-examples/tree/main/16_html-string
这是 Wrapper 组件的主体,它用于突出显示,但也使用 Fragment 组件进行渲染
---
const html = await Astro.slots.render('default');
import { Code } from 'astro/components';
---
<Fragment set:html={html} />
<Code code={html} lang="html" />
Run Code Online (Sandbox Code Playgroud)
用法如下
例如index.astro
---
import Card from '../components/Card.astro';
import StringWrapper from '../components/StringWrapper.astro';
...
---
<StringWrapper>
<Card title="Test" />
</StringWrapper>
Run Code Online (Sandbox Code Playgroud)
小智 0
Astro 组件在服务器上渲染,因此您可以直接引用包含的组件并根据需要传递 props。
// [title].astro
---
import Example from './components/Example.asto'
---
<Example message="Hey" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10883 次 |
| 最近记录: |