W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
// src/pages/todos/todos.json
{
"usingComponents": {
"add-button": "../../components/add-button/add-button",
"delete-check": "../../components/delete-check/delete-check"
}
}
4. 在需要使用到該組件的頁(yè)面 todos.wxml 中使用引入的組件
<!-- src/pages/todos/todos.wxml -->
<view class="page-todos">
...
<!-- 刪除確認(rèn)彈窗 -->
<delete-check wx:if="{{showDeleteCheck}}" checkIndex="{{checkIndex}}" onCancelDel="onCancelDel" />
</view>
1. 刪除入口:首頁(yè) src/pages/todos 需要添加一下刪除的觸發(fā)入口,修改一下首頁(yè)的樣式,js 中需要增加點(diǎn)擊刪除方法,并把該項(xiàng)的 index 傳給組件,用于標(biāo)記哪一項(xiàng)的內(nèi)容需要被刪除,同時(shí)添加一個(gè)方法 onCancelDel 用于設(shè)置 showDeleteCheck 為 false 關(guān)閉刪除確認(rèn)框。
<!-- src/pages/todos/todos.wxml -->
<label wx:for="{{todos}}" wx:for-item="item" class="todo-item {{item.completed ? 'checked' : ''}}" wx:key="{{index}}">
...
<view class="todo-item-del" bindtap="delTodo" data-index="{{index}}">刪除</view>
</label>
// src/pages/todos/todos.ts
import { wPage } from '@morjs/core'
wPage({
data: {
checkIndex: '',
showDeleteCheck: false
},
// 打開(kāi)刪除框
delTodo(e) {
const index = e.target?.targetDataset?.index || e.target?.dataset?.index
this.setData({
checkIndex: index,
showDeleteCheck: true
})
},
// 關(guān)閉刪除框
onCancelDel(e) {
this.setData({
todos: app.todos,
checkIndex: '',
showDeleteCheck: false
})
}
})
/* src/pages/todos/todos.less */
.todo-item-del {
margin: 0 20rpx;
color: crimson;
}
2. 刪除功能:
<!-- src/components/delete-check/delete-check.wxml -->
<view class="delete-check-bg">
<view class="delete-check">
<view class="delete-check__text">確認(rèn)刪除「{{text}}」</view>
<view class="delete-check__btn-box">
<view class="btn-del" bindtap="del">刪除</view>
<view class="btn-cancel" bindtap="cancel">取消</view>
</view>
</view>
</view>
// src/components/delete-check/delete-check.ts
import { wComponent } from '@morjs/core'
// 獲取全局 app 實(shí)例
const app = getApp()
wComponent({
props: {
checkIndex: '',
onCancelDel: () => {}
},
data: {
text: ''
},
created() {
const text = app.todos[this.props.checkIndex].text
this.setData({ text })
},
methods: {
del() {
app.todos.splice(this.props.checkIndex, 1)
this.cancel()
},
cancel() {
this.props.onCancelDel()
}
}
})
/* src/components/delete-check/delete-check.less */
.delete-check-bg {
width: 100vw;
height: 100vh;
background-color: rgba(#fff, 0.6);
position: fixed;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
.delete-check {
width: 400rpx;
border: 1rpx solid #ccc;
background: #fff;
border-radius: 8rpx;
}
.delete-check__text {
padding: 50rpx 30rpx;
text-align: center;
}
.delete-check__btn-box {
display: flex;
align-items: center;
.btn-del {
width: 50%;
text-align: center;
color: crimson;
padding: 20rpx;
border: 1rpx solid #ccc;
border-right: none;
}
.btn-cancel {
width: 50%;
text-align: center;
padding: 20rpx;
border: 1rpx solid #ccc;
}
}
}
// src/components/delete-check/delete-check.json
{
"component": true
}
3. 通過(guò)上述流程后,我們?cè)?todo list 的每一項(xiàng)后面都會(huì)有一個(gè)「刪除」按鈕,點(diǎn)擊將會(huì)打開(kāi)刪除彈窗,點(diǎn)擊刪除后即可刪除該項(xiàng),以上,恭喜你學(xué)會(huì)了怎么添加和編輯組件代碼!?? ?? ??
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: