簡單講:在js中使用app.terminate()
應用退出除使用物理按鍵退出應用外春贸,還可以通過組件的事件觸發(fā)。本章節(jié)以右滑退出為例遗遵,講述應用退出的操作萍恕。
應用退出,必須調(diào)用app模塊的terminate方法车要。當頁面右滑的時候會觸發(fā)onswipe事件允粤,在事件中執(zhí)行app.terminate即可實現(xiàn)應用退出。由于swipe是有方向屬性的翼岁,在事件函數(shù)處理中要注意判斷方向类垫,否則任意方向的滑動都會觸發(fā)退出應用操作,代碼示例如下:
<!-- index.hml:綁定div的swipe事件 -->
<div class="container" onswipe="touchMove">
<text id="title">
Hello {{title}}
</text>
<input type="button" value="View" style="width: 200px; height: 50px;" onclick="clickAction"></input>
</div>
// index.js:
import router from '@system.router'
// 導入app模塊
import app from '@system.app'
export default {
data: {
title: 'World'
},
clickAction(){
router.replace({
uri: 'pages/details/details'
});
},
touchMove(e){ // swipe處理事件
if(e.direction == "right") // 右滑退出
{
this.appExit();
}
},
appExit(){ // 退出app
app.terminate();
}
}