Vueff
by cedric_tarou
HTML
<div id="app">
<p>{{ text }}</p>
<button @click="removeFunc()">イベントを削除</button>
<button @click="addFunc()">イベントを付与</button>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
Vue
new Vue({
el: "#app",
data: {
text: "画面をクリック"
},
mounted() {
window.addEventListener("click",this.changeText);
},
methods: {
changeText() {
if(this.text === "画面をクリック") {
this.text = "変更したよ";
} else {
this.text = "画面をクリック";
}
},
addFunc() {
window.addEventListener("click", this.changeText);
},
removeFunc() {
window.removeEventListener("click", this.changeText);
},
}
})