既有项目没法重构,但想在某些页面的JS中使用vue的语法?不想手动调用小程序内的setData()?这个库可能帮你解决一些问题。javascript
使用ES6的proxy实现对page.data的代理html
欢迎提issue或pr :)
项目地址vue
$ git clone https://github.com/qk44077907/simple-wxmp-vueify.git
以后文件夹拷贝至项目根目录java
//你的路径 import '/simple-wxmp-vueify/index.js' Page({ reactive: true,//在初始化页面时传入此字段,库将自动把页面转化为响应式 data: { questionName: '', questionDesc: '', questionList: [ { content: '', }, { content: '', }, ], questionTypeList: ['单选', '多选'], questionTypeIndex: 0, }, computed: { questionType: function () { return this.questionTypeList[this.questionTypeIndex] } }, addItem() { this.questionList.push({ content: '', }) }, removeItem(e) { let index = e.target.dataset.index if (this.questionList.length <= 2) { return } this.questionList.splice(index, 1) } });
wxml模板react
<view class="question-panel"> <view class="item" wx:for="{{questionList}}" wx:for-item="question" wx:for-index="index" wx:key="name" > <!-- 注意双向绑定更新须要在对应元素上指定data-path属性 表明须要更新的属性相对data的路径,使用 '.'拼接 如 'questionList.0.content' --> <textarea bindinput="updateVM" data-path="questionList.{{index}}.content" type="text" placeholder="{{'选项'+(index+1)}}" placeholder-style="font-size:30rpx" maxlength="40" value="{{question.content}}" auto-height > </textarea> <view class="icon" bindtap="removeItem" data-index="{{index}}" > 删除 </view> <view class="icon" bindtap="addItem" data-index="{{index}}" > 添加 </view> </view> </view>