为什么我的Angular SPA没有识别谷歌教室共享按钮元素( )?

问题描述 投票:0回答:1

tl; dr我在尝试将google教室分享按钮添加到我的网络应用程序(角5,水疗中心)时收到以下错误:

Uncaught Error: Template parse errors:
':g:sharetoclassroom' is not a known element:
1. If ':g:sharetoclassroom' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
    </div>
    <div class="qr-code">
        [ERROR ->]<g:sharetoclassroom url="..." size="32"></g:sharetoclassroom>
    </div>
  </div>
"): ng:///AppModule/ShareLinkComponent.html@18:8

我想将google classroom share button添加到我的网络应用程序,但我无法弄清楚为什么没有识别共享按钮元素。我已将脚本(<script src="https://apis.google.com/js/platform.js" async defer></script>)添加到<head>index.html中。我已将share按钮元素添加到其中一个组件:

<!-- share-link.component.html -->

<mat-dialog-content>
  <div i18n class="share-link-text">
      Share the assessment link with your students.
  </div>
  <div class="share-container">
    <div class="share-to-classroom">
        <g:sharetoclassroom url="http://url-to-share" size="32"></g:sharetoclassroom>
    </div>
  </div>
</mat-dialog-content>

但是当我在本地运行代码时,我得到错误':g:sharetoclassroom' is not a known element。我真的不明白发生了什么/失踪了。奇怪的是,当我将元素添加到index.html(而不是任何其他组件)时,共享按钮实际上出现并按预期工作。任何人都知道加载第三方脚本/使用gapi /使用第三方元素(看似不是)需要添加到AppModule中的任何问题?

我已经尝试将脚本直接放在组件中,将NO_ERRORS_SCHEMA添加到@NgModule.schemas(它抑制了错误,但按钮根本没有出现,有0x0维...)。我没有创意,但会补充说我不是一个有角度的专家......

html angular google-classroom
1个回答
0
投票

如错误所示,将以下内容添加到模块中。问题是角度不能识别组件g:sharetoclassroom,因为它是一个web组件,在angular的范围之外声明。你需要通过NO_ERRORS_SCHEMA告诉棱角分明。这基本上允许您在该模块中添加外部Web组件。

@NgModule({
…
schemas: [
   NO_ERRORS_SCHEMA
]
})

编辑:我无法在您的代码中看到webcomponent的脚本被加载。请确保在webcomponent标记之后执行此操作(不使用asyncdefer)。

© www.soinside.com 2019 - 2024. All rights reserved.