<template>
</template>
<script>
import { getCurrentInstance } from 'vue';
export default {
name: 'App',
setup() {
const instance = getCurrentInstance();
console.log(instance);
}
}
</script>
import { getCurrentInstance, ComponentInternalInstance } from 'vue';
const { appContext } = <ComponentInternalInstance>getCurrentInstance()
console.log(appContext.config.globalProperties.$env);
// 推荐第二种方式
import {ref,reactive,getCurrentInstance} from 'vue'
const app = getCurrentInstance()
console.log(app?.proxy?.$filters.format('js'))
import { onMounted } from 'vue';
export default {
mounted() {}, // options api
setup() {
onMounted(() => {}); // composition api
}
}
updated
的方法即会调用<template>
<div>{{ count }}</div>
</template>
<script>
import {
getCurrentInstance,
ref,
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
watchEffect
} from 'vue';
export default {
name: 'App',
setup() {
const count = ref(0);
setTimeout(() => {
count.value = 1;
}, 1000);
watchEffect(() => {
console.log('watchEffect count value:', count.value);
});
onBeforeMount(() => console.log('onBeforeMount'));
onMounted(() => console.log('onMounted'));
onBeforeUpdate(() => console.log('onBeforeUpdate'));
onUpdated(() => console.log('onUpdated'));
return {
count
}
}
}
</script>
把 watchEffect 中的 flush 设置为 post 时,watch在 beforeUpdate 之后执行
watchEffect(() => {
console.log('watchEffect count value:', count.value);
}, {
flush: 'post'
});