最初data里面的isShowList 值设置为为false,默认隐藏状态。
<div @click="showList">点击切换出现或者消失</div>
<ul v-if="isShowList">
<li>aaa</li>
<li>bbb</li>
</ul>
showList() {
this.
isShowList = !
this.
isShowList;
},
autoHide(
evt) {
if
(!
this
.
$el
.
contains
(
evt
.
target
)) {
this.
isShowList =
false;
}
},
mounted() {
document.
addEventListener(
"click",
this.
autoHide,
false);
},
destroyed() {
document.
removeEventListener(
"click",
this.
autoHide,
false);
},