当前位置: 首页 > 后端技术 > Node.js

基于react-native&antd-mobile的三端开发

时间:2023-04-03 18:22:56 Node.js

做移动应用,同时适配ios、android、微信。寻找、试验、思考……几天内几经周折,我们最终决定使用react-native&antd-mobile来达到我们的目的。想法与选择搜索Internet并查看各种解决方案。第一种是使用redux共享业务逻辑,维护两套UI组件;二是先用react-native-web写好移动端,再把移动端转成H5;三是使用styled-components来封装UI组件,还要维护两套UI;第四:使用antd-mobile适配三端。最终还是决定选择antd-mobile方式,因为它本身就是一个很好的方案,文档齐全,实现简单。虽然有两套代码,但是已有的组件已经很多,扩展也很容易。我已经修复了一个小bug,发布到npm,并在项目中替换,这样可以快速方便的实现我想要的组件。代码编写原则所有界面元素均使用antd-mobile组件实现。如果不够或者不符合要求,直接修改antd-mobile。关键一步是在webpack2中配置antd-mobile支持H5,在webpack中配置,打包web版的代码。importantd-mobile{test:/\.(js|jsx)$/,exclude:/node_modules/,use:[{loader:'babel-loader',options:{presets:['es2015',"stage-2",'react'],plugins:[["transform-runtime"],["import",{libraryName:"antdm",style:true}]],}}],},resolveweb.*resolve:{mainFiles:["index.web","index"],//这里是modules:['app','node_modules',path.join(__dirname,'../node_modules')],extensions:['.web.tsx','.web.ts','.web.jsx','.web.js','.ts','.tsx','.js','.jsx','.react.js',],mainFields:['browser','jsnext:main','main',],},LayoutComponentsantd-mobile文档中只提到了复杂的组件,但是我们在H5中经常使用div和native,应该如何查看中处理?查看文档和搜索,我没有找到我想要的方法;我在github上看了别人回家代码,发现直接使用div或者原生View,无法同时适配三个终端,所以一度想引入styled-components进行封装,但总觉得引入styled-components只是用来处理一些基本元素。不值得。最后记得看antd-mobile的源码,在antd-mobile中引入了styled-components。这还不行吗?原来antd-mobile已经打包好了,也就是View。问题基本解决,但是运行时会出现如下警告信息:Warning:Unknownprop`Component`on

tag。从元素中删除此道具。有关详细信息,请参阅https://fb.me/react-unknown-propindiv(createdbyView)inView(atRoot.js:15)inProvider(atRoot.js:14)inRoot(atindex.js:20)inAppContainer(atindex.js:19)源码有个小bug,我修改,编译,运行,问题解决。提交pullrequests,别人更新快是不可能的,可能会有一些改动只是为了适应我们自己的项目,所以发布到npm,名字是antdm。该程序将有三个入口文件。原则是尽可能简单。'./src/containers/Root';从'./src/store/configureStore.js'导入configureStore;conststore=configureStore();classT3extendsComponent{render(){return();}}AppRegistry.registerComponent('t3',()=>T3);web:/src/web/index.jsimportReactfrom'react';import{render}from'react-dom';import{AppContainer}from'react-hot-loader';从'../containers/Root'导入根目录;从'../store/configureStore'导入configureStore;conststore=configureStore();render(,document.getElementById('root'))android:index.android.js还没写,应该和ios差不多。项目地址https://git.oschina.net/zhoutk/t3.githttps://github.com/zhoutk/t3.git使用gitclonehttps://git.oschina.net/zhoutk/t3.git或gitclonehttps://github.com/zoutk/t3.gitcdt3npmiios:npmruniosweb:npmrunwebsummary使用react-native和antd-mobie基本满足移动端适配三端的要求,但是同时工作在项目上,可能需要基于antd-mobile逐步搭建一套适合自己的UI组件。谢谢阿里兄弟!