<template>
<!-- 页面挂载时定义计时器,需要在页面销毁时清除定时器
可以通过 $on 或 $once 监听页面生命周期销毁来解决这个问题:
-->
<div class="hello">
<h1>{{ msg }}</h1>
<h1>{{ id }}</h1>
<p>程序化的事件侦听器</p>
</div>
</template>
<script>
export default {
name: "SkillFour",
props: ["id"],
data() {
return {
msg: "vue路由传参参数解耦"
};
},
mounted() {
this.creatInterval("hello");
this.creatInterval("world");
},
methods: {
creatInterval(msg) {
let timer = setInterval(() => {
console.log(msg);
}, 1000);
this.$once("hook:beforeDestroy", function() {
clearInterval(timer);
});
}
}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style >
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>