建立一个项目以前,首先须要选取合适的工具,目前使用比较广的两个weex脚手架有weexpack和weex-toolkit。css
weex init weex 建立项目html
修改weex.html文件,将./node_modules/weex-vue-render/index.js
修改成./node_modules/weex-vue-render/dist/index.js
vue
cnpm install 加载依赖包node
package.json中的scripts配置"app": "npm run build & npm run dev & npm run server"
android
npm run app 启动项目webpack
目录结构以下图:ios
weexpack create weex 建立项目web
weexpack platform add android 添加androidapache
weexpack platform add ios 添加iosnpm
weexpack run ios 模拟器运行
目录结构以下图:
由于咱们不打包android和ios,只须要将写好的页面打包成.weex.js文件供ios和android开发人员调用,因此采用了weex init的构建方式。
Weex Devtools是Weex开发调试必备的神器,安装好后,终端进入到项目目录,运行weex debug 会自动打开页面
扫二维码后
点击Inspector能够看页面信息,咱们打开Debugger,而后扫描打包好的js文件二维码就能够开始调试了。
注: 箭头所指处选debugger,我由于手贱选了个别的,致使好几天console里没有内容提示,还觉得版本问题,后来研究了下,发现这里选错了。
官方demo跑不通
高一点版本的weex-vue-render里index.js路径改变,致使。修改weex.html文件,将./node_modules/weex-vue-render/index.js
修改成./node_modules/weex-vue-render/dist/index.js
使用vue-resources获取接口数据, weex web上好的,可是weex-playground中跑不通,一片空白,错误信息:
[undefined:344:31] ReferenceError: Can't find variable: document addStyle addStylesToDom exports __webpack_require__ __webpack_require__ __webpack_require__ __webpack_require__ anonymous a@main.js:4:16690 main.js:7:8740
weex中不支持document和window,换成其它方式。weex不支持vue-resources,改为weex支持的fetch
<scroll>里loading一直没效果
<scroll>中使用refresh就无法用loading,去掉refresh模块
webpack报错,错误信息
ERROR in Entry module not found: Error: Cannot resolve 'file' or 'directory' /Users/xx/xx/code/weex/app.js in /Users/xx/xx/code/weex
开始一直觉得是webpack入口没配置对,检查不少遍,各类测试后,发现这里真的没问题
// entry: entries entry: { app: path.resolve('./app.js') }
后来找到问题出自
resolve: { extensions: ['.js', '.vue', '.json'] },
缘由是修改了默认的这个配置后,第一个空项不能省略,应该配置为
extensions: ['', '.js', '.vue', '.json'] },
错误信息
Cannot resolve module 'sass-loader'
缺乏node-sass 或 sass-loadernpm install node-sass sass-loader --save-dev
把sass-loader安装成了"scss-loader": "0.0.1",也是服了我本身。
接口地址只能获取本地数据,配置test环境失败
server.js中加一层代理
require('http-proxy-middleware') // api代理 var proxyTable = config.test.proxyTable Object.keys(proxyTable).forEach(function (context) { var options = proxyTable[context] if (typeof options === 'string') { options = { target: options } } server.use(proxyMiddleware(context, options)) }) // proxyTable数据 proxyTable: { '/api': { // 测试服务器 target: 'http://ip地址:端口号/xx', changeOrigin: true, pathRewrite: { '^/api': '' } }, ... }
weex接口调用,fetch的headers某些字段始终设置不上
fetch的headers只能设置下面这些字段
参考: https://developer.mozilla.org...
● 人为设置了对CORS安全的首部字段集合以外的其余首部字段。该集合为:
○ Accept ○ Accept-Language ○ Content-Language ○ Content-Type (but note the additional requirements below) ○ DPR ○ Downlink ○ Save-Data ○ Viewport-Width ○ Width
● Content-Type 的值不属于下列之一:
○ application/x-www-form-urlencoded ○ multipart/form-data ○ text/plain
stream的fetch使用get方式请求接口,url都会自动加上&undefined,官网的例子也不例外。本来普通接口多加一个undefined也没太大影响,可是咱们项目是须要根据url参数计算签名的,因此一直签名不经过。
找到源码出处
weex-vue-render第2753行对get进行了特别处理,第2764行的url拼接了body和hash,由于body没有传值,因此是undefined,注释掉url+=这行就没有undefined了,可是修改node_modules中的包内容显然不是一个合理的解决方案。
因而把get方式传值改成body传过来,web端好了,签名没有问题,可是真机上仍是报错,排查后发现问题出在get中使用了body传值,找到开发文档,
http://weex.apache.org/cn/ref...
而后我凌乱了,为何明明不能传body你的源码里又要有那么一行代码url += (config.url.indexOf('?') <= -1 ? '?' : '&') + body + hash;
。没办法,最后使用了一个超级笨的办法解决了。在签名计算的时候人为的给url加上“&undefined",计算好签名后,web中fetch参数中的url也要加上“&undefined",可是真机上是不会有&undefined的,因此真机上的url须要去掉undefined,好了问题解决了。
storage中的
getItem(key, callback)
封装后,页面没拿到数据。
storage异步形成的,使用promise解决
const p1 = new Promise(function (resolve) { storage.getItem(key, event => { let ls = event.data || '' let d = secret.decrypt(ls) // 对密文字符串进行解密。 d = typeof d === 'object' ? JSON.parse(d) : d resolve(d) }) }) Promise.all([p1]).then(function (result) { callback(result) }).catch(function(err){ console.log('error', err) })
页面跳转外部非js连接,在网页上是好的,但真机上一片空白
navigator.push({ url: 'https://segmentfault.com/write?freshman=1', animated: "true" })
新建一个vue文件,使用weex的web标签包一层,而后打包成weex.js格式,普通调用就行了。<web class="content" :src="url"></web>
跳转weex.js页面传参
直接在url后面拼接参数,新页面使用this.$getConfig().bundleUrl获取url解析一下就行了。
post提交数据的是后报错415
头部信息必定要和后端协议好,不容许不一致。