setup方法
Posted qisexin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了setup方法相关的知识,希望对你有一定的参考价值。
setup方法
Created: Oct 13, 2020 1:52 PM
功能: composition api
composition api
composition api为vue应用提供更好的逻辑复用和代码组织。
<template>
<div>
<p>counter: {{counter}}</p>
<p>doubleCounter: {{doubleCounter}}</p>
<p ref="desc"></p>
</div>
</template>
<script>
import {
reactive,
computed,
watch,
ref,
toRefs,
onMounted,
onUnmounted,
} from "vue";
export default {
setup() {
const data = reactive({
counter: 1,
doubleCounter: computed(() => data.counter * 2),
});
let timer
onMounted(() => {
timer = setInterval(() => {
data.counter++
}, 1000);
})
onUnmounted(() => {
clearInterval(timer)
})
const desc = ref(null)
watch(()=>data.counter, (val,oldVal)=>{
// console.log(`counter change from ${oldVal} to ${val}`);
desc.value.textContent = `counter change from ${oldVal} to ${val}`
})
return {...toRefs(data), desc};
},
};
</script>
以上是关于setup方法的主要内容,如果未能解决你的问题,请参考以下文章
我们可以在mapreduce代码中的mapper类的setup方法中放置一些计算任务吗