weex的实践踩坑日常(一)

先简单说下weex吧,官网的介绍是基于当代 Web 开发技术,使用同一套代码来构建 Android、iOS 和 Web 应用。具体来讲,在集成了 WeexSDK 之后,你可以使用 JavaScript 和现代流行的前端框架来开发移动应用。我个人当时认为是‘一套代码,三端都可使用’。在此之前考虑过reactNative,angular和ionic,但由于学习成本和项目需求,最后决定用weex。下面简单说下我这中间踩得一些坑吧。

1、由于我选得是最新的版本,所以根据官网的文档,故npm版本<5的用户需要通过npm i npm@latest -g更新一下npm的版本。版本低就会报错,报错信息上有提示的。

2、我用的weex的官网推荐的ui,Weex Ui。在项目中使用的话,最好使用最新的版本,旧的版本有一些功能模块有bug。

3、weex在使用之前,最好把官网文档读一遍。我是前端开发,选用的VUE开发。在使用过程中,首先weex中必须使用它本身提供的标签元素,不然解读的时候虽然在浏览器端虽然你也可以看到效果,但是手机上是看不到的。因为浏览器的特性在手机上不一定能适用。第二css样式,有些css3的一些简单写法是不支持的,在weex中需要把每个样式都写出来,不要简写。在weex中默认的是flex弹性布局,如果有不懂的最好去看下看几个博客,看一遍基本上就可以了。这里推荐一篇很通俗易懂的日志Flex。在weex中默认的flex-direction是column,内容是垂直排列的,如果需要从左到右排列,需要自己更改flex-direction为row。还有背景图片,可以用image标签加定位实现,或者用background-image这个也可以,不过最好使用网络资源加载,它还可以用来写渐变色。在使用定位(绝对,相对,固定)时,注意z-index是不生效,这个在浏览器中是可以看到效果得,但是在手机上是没用的。在手机上元素的层级,是写在后面的比前面的层级高。第三,在weex中事件时,要看清楚有哪些事件时支持的,不是所有js事件都是可以使用的。第四,在weex中使用vue时要注意哪些语法是不支持的,在weex中使用vue。第五,在weex中宽高是固定的,只支持px写法,比如宽是750px,那么在编译过程中就会默认宽度为100%。页面的跳转,暂时我用的是路由跳转,原生的不是很懂。网上也有不少例子,可以下载下来,看下里面的一些方法,还是很不错的。Weex Market上面有不少不错的插件可以,有需要的可以打开看看。

暂时就这么多了,有什么不足之处,请大神多多指教。

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要封装一个weex手势解锁插件,可以按照以下步骤进行: 1. 首先,在weex项目中创建一个自定义组件GestureLock.vue,用于显示手势解锁界面。 2. 在GestureLock.vue中,引入一些必要的组件: ```html <template> <div class="gesture-lock"> <canvas ref="canvas" class="canvas"></canvas> <div class="tip">{{tip}}</div> </div> </template> <script> import { mapState } from 'vuex' import { getTouchPosition } from '@/utils' export default { computed: { ...mapState({ gesturePassword: state => state.user.gesturePassword }) }, data () { return { tip: '请绘制解锁图案', start: false, moves: [], lineWidth: 4, canvasWidth: 300, canvasHeight: 300 } }, mounted () { this.init() }, methods: { init () { this.canvas = this.$refs.canvas this.ctx = this.canvas.getContext('2d') this.ctx.lineWidth = this.lineWidth this.ctx.strokeStyle = '#ffffff' this.draw() }, draw () { this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight) this.drawPath() this.drawLines() }, drawPath () { if (this.moves.length === 0) { return } this.ctx.beginPath() this.ctx.moveTo(this.moves[0].x, this.moves[0].y) for (let i = 1; i < this.moves.length; i++) { this.ctx.lineTo(this.moves[i].x, this.moves[i].y) } this.ctx.stroke() this.ctx.closePath() }, drawLines () { this.ctx.beginPath() for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { const index = i * 3 + j if (this.moves.includes(index)) { this.ctx.fillStyle = '#ffffff' } else { this.ctx.fillStyle = '#ffffff11' } this.ctx.arc(this.canvasWidth / 6 * (2 * j + 1), this.canvasHeight / 6 * (2 * i + 1), this.canvasWidth / 12, 0, Math.PI * 2, true) this.ctx.fill() } } this.ctx.closePath() }, startDraw () { this.start = true this.tip = '请继续绘制图案' }, moveDraw (event) { if (!this.start) { return } event.preventDefault() const {x, y} = getTouchPosition(event) const index = this.getIndex(x, y) if (index !== -1 && !this.moves.includes(index)) { this.moves.push(index) this.draw() } }, endDraw () { this.start = false if (this.moves.length < 4) { this.tip = '至少连接4个点,请重新绘制' this.clearPath() return } if (this.gesturePassword) { if (this.moves.join('') === this.gesturePassword) { this.tip = '解锁成功' } else { this.tip = '解锁失败,请重新绘制' this.clearPath() } } else { this.$store.dispatch('user/setGesturePassword', this.moves.join('')) this.tip = '请再次绘制解锁图案' this.clearPath() } }, clearPath () { this.moves = [] setTimeout(() => { this.draw() }, 500) }, getIndex (x, y) { if (x < 0 || x > this.canvasWidth || y < 0 || y > this.canvasHeight) { return -1 } const col = Math.floor(x / (this.canvasWidth / 3)) const row = Math.floor(y / (this.canvasHeight / 3)) return row * 3 + col } } } ``` 在上述代码中,我们使用了canvas来绘制手势解锁界面,并定义了一些相关方法,如draw、drawPath、drawLines等。其中,draw方法用于绘制整个界面,drawPath方法用于绘制手势路径,drawLines方法用于绘制手势路径之外的圆圈。startDraw、moveDraw、endDraw方法分别用于手势的开始、移动、结束事件处理,getIndex方法用于获取手势所在的圆圈。 3. 在weex项目中创建一个store.js,用于管理用户信息。 ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { user: { gesturePassword: '' } }, mutations: { SET_GESTURE_PASSWORD: (state, gesturePassword) => { state.user.gesturePassword = gesturePassword } }, actions: { setGesturePassword ({commit}, gesturePassword) { commit('SET_GESTURE_PASSWORD', gesturePassword) } } }) ``` 在store.js中,我们定义了一个user对象,用于存储用户信息,包括手势密码。同时,我们也定义了一个setGesturePassword方法,用于设置手势密码。 4. 在weex项目中的入口文件main.js中引入store.js,并将store.js注入到Vue对象中。 ```javascript import Vue from 'vue' import App from './App.vue' import store from './store' Vue.config.productionTip = false new Vue({ store, render: h => h(App) }).$start() ``` 5. 在weex项目中,将GestureLock组件作为一个路由页面,并在路由配置文件router.js中进行配置。 ```javascript import GestureLock from '@/components/GestureLock' export default [ { path: '/', component: GestureLock } ] ``` 6. 最后,在weex项目中运行npm run build命令进行打包,打包完成后,将dist目录下的文件上传到weex平台即可使用。 以上就是封装一个weex手势解锁插件的全部步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值