vuejs / composition-api

Composition API plugin for Vue 2

Home Page:https://composition-api.vuejs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

why setup run twice in vue2

zhoufanglu opened this issue · comments

commented

i install @vue/composition-api at vue 2.6。

  • in setup
<script>
export default {
  setup() {  // run twice
    console.log('test composition =================')
  }
}
</script>
  • in created
  created() {  // run  at a time
    console.log('test composition =================')
  }

Can you provide me a reproduction demo?

commented

Can you provide me a reproduction demo?

sorry, this is company project, rather old, later i use vue2 syntax

I have the same problem with vue 2 and composition api. But in onMounted hook code runs once.

Is there any progress on this issue?
For reproducing, Just create a vue 2 project and write a component in composition API. The setup would be called twice.
Below is a simple vue for reproducing this issue:

<script setup lang="ts">
import {ref} from "@vue/composition-api";

const a = ref('aa')
const b = ref('bb')
console.log('call setup')
</script>

<template lang="pug">
span haha {{a}} {{b}}
</template>

Hey @xiaoxiangmoe ,

i just ran into the exact same issue using vue 2.7.14 and composition-api@1.7.2

here is a Reproduction Demo: https://codesandbox.io/s/confident-architecture-p6jm5p?file=/src/App.vue

Just have a look at the terminal. You'll find Setup being printed twice, mounted only once.

Hi,@marcel-wtfoxtrot
In vue 2.7, you needn't use composition-api, just use vue

Have you found the reason?