ERROR TypeError: i.createShadowRoot is not a function on Angular Web Component?
ERROR TypeError: i.createShadowRoot is not a function on Angular Web Component?
我正在尝试一个基本的 Angular Web 组件。我创建了一个 github 存储库,在此处显示了该方法:
https://github.com/fireflysemantics/fs-gist
这是组件:
https://github.com/fireflysemantics/fs-gist/blob/master/src/app/fs-gist/fs-gist.component.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'fs-gist',
template: `
<p>
fs-gist works!
</p>
`,
styles: [
],
encapsulation: ViewEncapsulation.Native
})
export class FsGistComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
这是模块:
https://github.com/fireflysemantics/fs-gist/blob/master/src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FsGistComponent } from './fs-gist/fs-gist.component';
import { Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
@NgModule({
declarations: [
FsGistComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [FsGistComponent]
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const el = createCustomElement(
FsGistComponent,
{ injector: this.injector });
customElements.define('fs-gist', el);
}
}
这些是 package.json
中的构建脚本
https://github.com/fireflysemantics/fs-gist/blob/master/package.json
"b": "ng build --prod --output-hashing=none",
"c": "bash -c 'cat dist/fs-gist/{runtime-es5,polyfills-es5,main-es5}.js > fs-gist.js'",
"c1": "bash -c 'cat dist/fs-gist/{runtime-es2015,polyfills-es2015,main-es2015}.js > fs-gist2015.js'",
"p": "npm run b && npm run c",
"p1": "npm run b && npm run c1",
"cp-fs-gist": "cp fs-gist.js ../wctest/src/assets/js/"
运行 npm run p
将在根文件夹中创建 Web 组件 fs-gist
。
然后我将其复制到此测试项目的 src/assets/js/
文件夹中:
https://github.com/fireflysemantics/wc-test
加载应用程序时,控制台记录:
fs-gist.js:1 ERROR TypeError: i.createShadowRoot is not a function
at new n (fs-gist.js:1)
at e.value (fs-gist.js:1)
at fs-gist.js:1
at n.value (fs-gist.js:1)
at e.value (fs-gist.js:1)
at e.value (fs-gist.js:1)
at HTMLElement.value (fs-gist.js:1)
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (fs-gist.js:1)
at ZoneDelegate.invoke (zone-evergreen.js:363)
我在 polyfills.js
:
中包含了 Web 组件的 polyfill
import "@webcomponents/custom-elements/src/native-shim";
import "@webcomponents/custom-elements/custom-elements.min";
import '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js'
并且我已将 Web 组件加载添加到 index.html
:
<script src="webcomponents/webcomponents-loader.js"></script>
<script>
if ( !window.customElements )
{ document.write('<!--') }
</script>
<script src="webcomponents/custom-elements-es5-adapter.js"></script>
<!-- ! KEEP ME -->
有什么想法吗?
另外,为了看看我是否可以在最小的环境中运行它,我使用了第一个项目生成的 fs-gist
Web 组件,并用它制作了一个最小的 javascript stackblitz。
该演示从 CDN 导入网络组件 polyfill。
就是这样:
https://stackblitz.com/edit/js-custome-element-test
产生以下错误:
ERROR
Error: Failed to execute 'define' on 'CustomElementRegistry': the name "fs-gist" has already been used with this registry
错误报告
我认为 Web 组件的 Angular 编译存在错误。
View Encapsulation组件需要ViewEncapsulation.ShadowDom
(参考上面提到的错误报告):
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'fs-gist',
template: `
<p>
fs-gist works!
</p>
`,
styles: [
],
encapsulation: ViewEncapsulation.ShadowDom
})
export class FsGistComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
我正在尝试一个基本的 Angular Web 组件。我创建了一个 github 存储库,在此处显示了该方法:
https://github.com/fireflysemantics/fs-gist
这是组件: https://github.com/fireflysemantics/fs-gist/blob/master/src/app/fs-gist/fs-gist.component.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'fs-gist',
template: `
<p>
fs-gist works!
</p>
`,
styles: [
],
encapsulation: ViewEncapsulation.Native
})
export class FsGistComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
这是模块: https://github.com/fireflysemantics/fs-gist/blob/master/src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FsGistComponent } from './fs-gist/fs-gist.component';
import { Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
@NgModule({
declarations: [
FsGistComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [FsGistComponent]
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const el = createCustomElement(
FsGistComponent,
{ injector: this.injector });
customElements.define('fs-gist', el);
}
}
这些是 package.json
https://github.com/fireflysemantics/fs-gist/blob/master/package.json
"b": "ng build --prod --output-hashing=none",
"c": "bash -c 'cat dist/fs-gist/{runtime-es5,polyfills-es5,main-es5}.js > fs-gist.js'",
"c1": "bash -c 'cat dist/fs-gist/{runtime-es2015,polyfills-es2015,main-es2015}.js > fs-gist2015.js'",
"p": "npm run b && npm run c",
"p1": "npm run b && npm run c1",
"cp-fs-gist": "cp fs-gist.js ../wctest/src/assets/js/"
运行 npm run p
将在根文件夹中创建 Web 组件 fs-gist
。
然后我将其复制到此测试项目的 src/assets/js/
文件夹中:
https://github.com/fireflysemantics/wc-test
加载应用程序时,控制台记录:
fs-gist.js:1 ERROR TypeError: i.createShadowRoot is not a function at new n (fs-gist.js:1) at e.value (fs-gist.js:1) at fs-gist.js:1 at n.value (fs-gist.js:1) at e.value (fs-gist.js:1) at e.value (fs-gist.js:1) at HTMLElement.value (fs-gist.js:1) at ZoneDelegate.invoke (zone-evergreen.js:364) at Object.onInvoke (fs-gist.js:1) at ZoneDelegate.invoke (zone-evergreen.js:363)
我在 polyfills.js
:
import "@webcomponents/custom-elements/src/native-shim";
import "@webcomponents/custom-elements/custom-elements.min";
import '@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js'
并且我已将 Web 组件加载添加到 index.html
:
<script src="webcomponents/webcomponents-loader.js"></script>
<script>
if ( !window.customElements )
{ document.write('<!--') }
</script>
<script src="webcomponents/custom-elements-es5-adapter.js"></script>
<!-- ! KEEP ME -->
有什么想法吗?
另外,为了看看我是否可以在最小的环境中运行它,我使用了第一个项目生成的 fs-gist
Web 组件,并用它制作了一个最小的 javascript stackblitz。
该演示从 CDN 导入网络组件 polyfill。
就是这样:
https://stackblitz.com/edit/js-custome-element-test
产生以下错误:
ERROR Error: Failed to execute 'define' on 'CustomElementRegistry': the name "fs-gist" has already been used with this registry
错误报告
我认为 Web 组件的 Angular 编译存在错误。
View Encapsulation组件需要ViewEncapsulation.ShadowDom
(参考上面提到的错误报告):
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'fs-gist',
template: `
<p>
fs-gist works!
</p>
`,
styles: [
],
encapsulation: ViewEncapsulation.ShadowDom
})
export class FsGistComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}