簡體   English   中英

使用 react 和 typescript 添加標簽腳本

[英]Adding tag script with react and typescript

我正在嘗試將 Spotify 中的腳本標記添加到頁面。 為此,我正在使用useEffect ,但是當我嘗試獲取 Spotify 類時,找不到它(我使用的是 ReactJS 和 Typescript):

useEffect(() => {
        const script = document.createElement('script');
      
        script.src = "https://sdk.scdn.co/spotify-player.js";
        script.async = true;
      
        document.body.appendChild(script);
      
        return () => {
          document.body.removeChild(script);
        }
      }, []);

(window as any).onSpotifyWebPlaybackSDKReady = () => {
        const token = '[My Spotify Web API access token]';
        const player = new Spotify.Player({
          name: 'Web Playback SDK Quick Start Player',
          getOAuthToken: cb => { cb(token); }
        });

在此處輸入圖片說明

對於添加了script標簽的外部腳本,應該在全局范圍內定義一個typescript能夠識別的變量。

declare var Spotify:any;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM