代码之家  ›  专栏  ›  技术社区  ›  Flignats

角度7:在组件中使用外部脚本

  •  0
  • Flignats  · 技术社区  · 6 年前

    我正在尝试使用TwitchAPI在我的web应用程序中嵌入频道。

    说明如下:

    <html>
      <body>
        <!-- Add a placeholder for the Twitch embed -->
        <div id="twitch-embed"></div>
    
        <!-- Load the Twitch embed script -->
        <script src="https://embed.twitch.tv/embed/v1.js"></script>
    
        <!-- Create a Twitch.Embed object that will render within the "twitch-embed" root element. -->
        <script type="text/javascript">
          new Twitch.Embed("twitch-embed", {
            width: 854,
            height: 480,
            channel: "monstercat"
          });
        </script>
      </body>
    </html>
    

    我包括了 https://embed.twitch.tv/embed/v1.js 我的文件 assets 文件夹和angular.json。

        "scripts": [
          "src/assets/twitch/twitch-embed-v1.js"
        ]
    

    我的组件文件如下所示:

    import Twitch from '../../../../../assets/twitch/twitch-embed-v1';
    
    export class TwitchPlayerComponent implements OnInit {
    
      ngOnInit() {
        const options = {
          width: 854,
          height: 480,
          channel: '424976424',
        };
        const player = new Twitch.Embed('twitch-embed', options);
        player.setVolume(0.5);
      }
    
    }
    

    该脚本出现在Chrome devtools的“我的源代码”面板中,但出现以下错误:

    Embed is not a constructor

    正确使用脚本的障碍是什么?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Poul Kruijt    6 年前

    twitch的v1还没有输入类型脚本,这将在v2中出现。不应将脚本添加到资产文件夹中。如果twitch有更新怎么办?您必须手动更新资产文件夹。

    无论如何,我认为,要使用twitch SDK,您可以执行以下操作:

    npm install twitch-js@next
    

    在您的TS中:

    import * as Twitch from 'twitch-js';
    
    new Twitch.Embed({...});