在Accomazzo的React Fullstack一书中,他们在第一章使用seed.js作为数据源。它有生命周期,他们使用窗口对象在项目中的任何地方使用它。但是,当我创建自己的react项目并遵循代码时,它显示了一个错误,说Seed is not defined no-undef
我尝试过导入,包括html文件中的js文件,但似乎不起作用
这是app.js文件
import React from 'react';
class PlayerList extends React.Component {
render() {
const player = Seed.players[0]
return (
<div className='ui unstackable items'>
<Player
id={player.id}
name={player.name}
club={player.club}
url={player.url}
votes={player.votes}
submitterAvatarUrl={player.submitterAvatarUrl}
playerImgUrl={player.playerImgUrl}
/>
</div>
)
}
}这是seed.js文件
window.Seed = (function () {
function generateVoteCount() {
return Math.floor((Math.random() * 50) + 15);
}
const players = [
{
id: 1,
name: 'CR7',
club: 'Juventus',
url: '#',
votes: generateVoteCount(),
submitterAvatarUrl: 'images/avatars/profile-pic.jpeg',
playerImgUrl: 'images/players/CR7.jpg',
},
{
id: 2,
name: 'LM10',
club: 'Barcelona',
url: '#',
votes: generateVoteCount(),
submitterAvatarUrl: 'images/avatars/kristy.png',
playerImgUrl: 'images/players/LM10.jp',
},
{
id: 3,
name: 'MS10',
club: 'Liverpool',
url: '#',
votes: generateVoteCount(),
submitterAvatarUrl: 'images/avatars/veronika.jpg',
playerImgUrl: 'images/players/MS10.jpg',
},
];
return { players: players };
}());发布于 2019-08-02 14:11:34
在这里您可以找到一个工作示例,只需导入您的种子文件。基本上,您只需要import 'seed'并添加window.Seed以避免错误
https://stackoverflow.com/questions/57320691
复制相似问题