react技术栈仿App版网易云音乐
react-music本来没打算写网易云音乐的,好像都已经被大家写烂了,不过没办法,暂时想不到其他的可写,加上网易云音乐又有API,还可以基于restful API做一层graphql的处理再提供给前端调用,然后才决定用react写了这个仿app版网易云音乐 技术栈
实现的功能
运行git clone git@github.com:Binaryify/NeteaseCloudMusicApi.git 这是网易云API,因为最新的banner数据已经改了,可以 cross-env PORT=8080 npm start 首先启动api服务器,需要用 git clone git@github.com:Kim09AI/react-music.git # dev模式 # 先启动graphql服务器 $ cd server && npm run dev # 再回到根目录 $ npm start # production模式 # 首次build前先执行(因为使用了dll) $ npm run build:dll $ npm run build # 本地以production模式启动服务器 cd server && npm start 预览线上地址 一些细节路由配置react并没有像vue那样,可以通过配置routes数组对象,就会帮我们生成好路由,那要是想像vue一样使用,就得自己动手来实现一个了 /** * 根据路由配置生成相应路由 * @param {array} routeConfig 路由配置 * @param {string} parentPath 父级路由 */ export function routes(routeConfig,parentPath = '') { if (!routeConfig || routeConfig.length === 0) { return null } return ( routeConfig.map(route => ( <Route path={parentPath + route.path} key={parentPath + route.path} exact={route.exact} render={(props) => ( <route.component {...props}> {/* 在父级路由通过 this.props.children 即可添加嵌套路由*/} {routes(route.routes,parentPath + route.path)} </route.component> )} /> )) ) } 调用方式 <Router> <Switch> { routes(routeConfig) } <Redirect to="/home" /> </Switch> </Router> 对应的routeConfig配置 const routeConfig = [ { path: '/home',component: Home,routes: [ { path: '/',component: Found,exact: true },{ path: '/mime',component: Mime },{ path: '/radio',component: Radio } ] },{ path: '/search',component: Search,exact: true },{ path: '/search/:keywords',component: Search },{ path: '/playlistDetail/:id',component: PlaylistDetail },{ path: '/radioDetail/:rid',component: RadioDetail } ] export default routeConfig 其实主要的还是如何配置嵌套路由,核心是在配置Route时不使用 路由懒加载react不像vue一样,可以在设置Route的component的时候返回一个promise,等resolve后再路由跳转,这里我使用的是通过一个高阶组件,在这个高阶组件的 componentDidMount() { load().then(({ default: component }) => { this.setState({ Component: component }) }) } 懒加载主要就是为了更快的加载首屏,但是在react中有这样一个问题,因为不能在component处返回一个promise,一切换路由就会马上跳转到高阶组件,而 <link rel="prefetch" href="/static/js/pagePlaylistDetail.910ef6d3.chunk.js"> 这里用了一个插件 new PreloadWebpackPlugin({ rel: 'prefetch' }), 移动端无法autoplay刚开始是使用网上广为流传的 友情提示在手机上安装谷歌浏览器,访问网站后,点击设置,点击添加到主屏幕,就可以像访问app一样在手机上访问项目了,因为 react使用的一些总结主要还是在react-redux的使用了,数据应该保存在state还是全局的store,主要还是看数据需不需要共享,或者是需不需要缓存,不然存在store反而会使问题变得更麻烦 最后感谢 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |