我在应用程序中使用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中。package.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.json
和node_modules
并运行npm install
关于reactjs - react -未捕获的TypeError : Cannot read property 'Component' of undefined,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58124796/