简体   繁体   English

StencilJS 在 HTML 文件中使用组件

[英]StencilJS using component in HTML file

I want to use a component created using StencilJS in a regular basic HTML file.我想在常规的基本 HTML 文件中使用使用 StencilJS 创建的组件。 I followed these steps:我遵循了以下步骤:

I have created a stencil component to create the basic my-component example:我创建了一个模板组件来创建基本的 my-component 示例:

npm init stencil

I want to use this component in an HTML file, so I ran我想在 HTML 文件中使用这个组件,所以我运行了

npm run build

I then created an html project with the following structure:然后我创建了一个具有以下结构的 html 项目:

在此处输入图像描述

And then I moved the files from the dist folder into the script folder.然后我将文件从 dist 文件夹移动到脚本文件夹中。 And I added script tag in the head of my html file that references the component.js file like this:我在引用 component.js 文件的 html 文件的头部添加了脚本标签,如下所示:

<script src="script/{component_name}/{component_name}.js"></script>

And I used the component in the html like this:我在 html 中使用了这样的组件:

<my-component first="Stencil" last="'Don't call me a framework' JS"></my-component>

But my component isn't being rendered.但是我的组件没有被渲染。 I get an error involving a esm.js file.我收到一个涉及 esm.js 文件的错误。 Can someone help me with this process of compiling my stencil component to be using in a basic HTML project?有人可以帮助我完成编译我的模板组件以用于基本 HTML 项目的过程吗?

Stencil bundles your dist into modules and lazy-loads only the required code based on the components you are actually using in your HTML. Stencil 将您的 dist 捆绑到模块中,并根据您在 HTML 中实际使用的组件仅延迟加载所需的代码。 So you should serve the whole dist folder along with your website.因此,您应该将整个dist文件夹与您的网站一起提供。

The recommended way is to have the following two script tags in your html file:推荐的方法是在您的 html 文件中包含以下两个脚本标记:

<script type="module" src="/dist/[namespace]/[namespace].esm.js"></script>
<script nomodule src="/dist/[namespace]/[namespace].js"></script>

(where [namespace] is whatever is set in your stencil.config.ts ) (其中[namespace]是您的stencil.config.ts中设置的任何内容)

This will instruct browsers who support ES Modules to use the esm bundle, and other browsers will use the ES5 (cjs) bundle.这将指示支持 ES 模块的浏览器使用 esm 包,而其他浏览器将使用 ES5 (cjs) 包。

If my-component is the only component that you're using from your library, then only that code will be lazy-loaded by your page.如果my-component是您从库中使用的唯一组件,那么您的页面只会延迟加载该代码。 Stencil knows about component interdependencies and how to lazy-load them accordingly. Stencil 了解组件的相互依赖性以及如何相应地延迟加载它们。


There is a new experimental output target (called custom-elements-bundle ) that allows you to bundle everything into one js file, which will simplify distribution in some cases.有一个新的实验性输出目标(称为custom-elements-bundle ),它允许您将所有内容捆绑到一个 js 文件中,这将在某些情况下简化分发。 It's only available with the new refactored compiler (which is available using the --next flag, after installing @stencil/core@next ) (Stencil 2 has been out for a while now). 它仅适用于新的重构编译器(安装 @stencil/core@next后使用 --next标志可用) (Stencil 2 已经推出一段时间了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM