我正在使用 react-media-player作为我的应用程序中的播放器。我是这样安装的:

npm install react-media-player --save 

在 index.html 处:
<script src="https://unpkg.com/react-media-player/dist/react-media-player.js"></script> 
//(UMD library exposed as `ReactMediaPlayer`) 

这是我将模块导入组件的代码:
import React, { Component } from 'react'; 
import axios from 'axios'; 
import { withRouter } from 'react-router-dom'; 
import { Media, Player, controls } from 'react-media-player' 
 
 
 
class Tracks extends Component{ 
  constructor (props) { 
    super(props); 
    this.state = {       
        artists:[], 
        titles:[], 
        energies: [], 
        genres: [], 
        popularities:[], 
        images:[], 
        previews:[], 
        template:'' 
    }; 
  }; 
 
  componentDidMount() { 
    if (this.props.isAuthenticated) { 
      this.getTrack(); 
    } 
  }; 
 
  getCoffee(event) { 
    const {select} = this.props 
    const options = { 
      url: `${process.env.REACT_APP_WEB_SERVICE_URL}/track/${select}`, 
      method: 'get', 
      headers: { 
        'Content-Type': 'application/json', 
        Authorization: `Bearer ${window.localStorage.authToken}` 
      } 
    }; 
    return axios(options) 
    .then((res) => { 
      console.log(res.data.data) 
      console.log(select) 
      this.setState({ 
        playlist: res.data.data[0].playlist, 
        artists: res.data.data[0].artists, 
        titles: res.data.data[0].titles, 
        energies: res.data.data[0].energies, 
        genres: res.data.data[0].genres, 
        popularities: res.data.data[0].popularities, 
        images: res.data.data[0].images, 
        previews: res.data.data[0].previews, 
        template: res.data.data[0].content, 
      }) 
    })     
    .catch((error) => { console.log(error); }); 
  }; 
 
  Capitalize(str){ 
    return str.charAt(0).toUpperCase() + str.slice(1); 
    } 
 
  render(){ 
    const { select } = this.props 
    const { PlayPause, CurrentTime, Progress, Duration, MuteUnmute } = controls 
    const { artists, titles, energies, genres, popularities, images, previews } = this.state; 
 
    return ( 
      <> 
        {  
          artists.map((artist, index) => { 
            /*  
            Obtain preview from state.previews for current artist index  
            */ 
            const title = titles[index]; 
            const energy = energies[index]; 
            const genre = genres[index]; 
            const popularity = popularities[index]; 
            const image = images[index]; 
            const preview = previews[index]; 
            /*  
            Render current artist data, and corresponding preview data 
            for the current artist  
            */ 
            return ( 
              <div> 
                    <span key={index}> 
                      <tr>  
                        <img src={image}  alt="" height="42" width="42" /> 
                        <td class="number">{ artist }</td> 
                        <td class="number">{ title }</td> 
                          <Media> 
                            <div className="media"> 
                              <div className="media-player"> 
                                <Player src={preview} /> 
                              </div> 
                              <div className="media-controls"> 
                                <PlayPause /> 
                                <CurrentTime /> 
                                <Progress /> 
                                <Duration /> 
                                <MuteUnmute /> 
                              </div> 
                            </div> 
                          </Media> 
                      </tr> 
                    </span> 
              </div> 
            ) 
          }) 
        } 
      </>    
    ) 
  } 
} 
 
export default withRouter(Tracks); 

但我遇到了一个奇怪的行为。每次我停止客户端容器时,都会收到以下错误:
Failed to compile 
./src/components/Tracks.jsx 
Module not found: Can't resolve 'react-media-player' in '/usr/src/app/src/components' 

但是,过了一会儿,当我更改一些代码并保存更改时,它会编译并开始工作。仅在客户端重新启动时再次中断:

这是我的控制台一直显示的内容(即使它正在工作):
react-media-player.js:344 Uncaught TypeError: Cannot read property 'Component' of undefined 
    at Object.<anonymous> (react-media-player.js:344) 
    at __webpack_require__ (react-media-player.js:35) 
    at Object.<anonymous> (react-media-player.js:69) 
    at __webpack_require__ (react-media-player.js:35) 
    at react-media-player.js:55 
    at react-media-player.js:58 
    at webpackUniversalModuleDefinition (react-media-player.js:14) 
    at react-media-player.js:15 

这就是控制台在抛出编译错误时向我显示的内容:
Brewing.jsx:21 Uncaught Error: Cannot find module 'react-media-player' 
    at webpackMissingModule (Brewing.jsx:21) 
    at Module../src/components/Tracks.jsx (Brewing.jsx:21) 
    at __webpack_require__ (bootstrap:781) 
    at fn (bootstrap:149) 
    at Module../src/App.jsx (Spotify.css:4) 
    at __webpack_require__ (bootstrap:781) 
    at fn (bootstrap:149) 
    at Module../src/index.js (spotify-auth.js:8) 
    at __webpack_require__ (bootstrap:781) 
    at fn (bootstrap:149) 
    at Object.0 (index.js:10) 
    at __webpack_require__ (bootstrap:781) 
    at checkDeferredModules (bootstrap:45) 
    at Array.webpackJsonpCallback [as push] (bootstrap:32) 
    at main.chunk.js:1 

这里有什么问题?

旁注 : 我删除了 package-lock.json并重新安装了模块,但无济于事。 react-media-player存在于/node_modules。

包.json:
{ 
  "name": "client", 
  "version": "0.1.0", 
  "private": true, 
  "dependencies": { 
    "axios": "^0.18.0", 
    "react": "^16.8.6", 
    "react-dom": "^16.8.6", 
    "react-hot-loader": "^4.5.3", 
    "react-html-parser": "^2.0.2", 
    "react-media-player": "^0.7.7", 
    "react-router-dom": "^5.0.0", 
    "react-scripts": "3.0.1", 
    "spotify-web-api-js": "^1.2.0" 
  }, 
  "scripts": { 
    "start": "react-scripts start", 
    "build": "react-scripts build", 
    "test": "react-scripts test", 
    "eject": "react-scripts eject" 
  }, 
  "eslintConfig": { 
    "extends": "react-app" 
  }, 
  "browserslist": { 
    "production": [ 
      ">0.2%", 
      "not dead", 
      "not op_mini all" 
    ], 
    "development": [ 
      "last 1 chrome version", 
      "last 1 firefox version", 
      "last 1 safari version" 
    ] 
  }, 
  "devDependencies": { 
    "enzyme": "^3.9.0", 
    "enzyme-adapter-react-16": "^1.11.2" 
  } 
} 

请您参考如下方法:

您需要删除

<script src="https://unpkg.com/react-media-player/dist/react-media-player.js"></script> 
//(UMD library exposed as `ReactMediaPlayer`) 

来自 index.html .仅当您使用 react-media-player throw CDN 时。

也尝试删除 package-lock.jsonnode_modules并运行 npm install


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!