Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reactive is working not consistent with playground #5741

Closed
asialiugf opened this issue Apr 18, 2022 · 5 comments
Closed

reactive is working not consistent with playground #5741

asialiugf opened this issue Apr 18, 2022 · 5 comments

Comments

@asialiugf
Copy link

Version

3.2.33

Reproduction link

sfc.vuejs.org/

Steps to reproduce

  1. npm init vite@latest : create a new project (or npm init vue@latest : select vue ts)
  2. copy the playground App.vue to the project's App.vue
  3. run it !

App.vue

obj:{{ obj }}
reactiveObj:{{ reactiveObj }}
reactiveObj1:{{ reactiveObj1 }}
修改obj
修改reactive
修改reactive1
<script setup lang="ts"> import { reactive, ref, toRefs } from "vue"; import { defineComponent } from "@vue/runtime-core"; let obj = { value: 0 }; let obj1 = { value: 0 }; let reactiveObj = reactive<{ value: number }>(obj); const reactiveObj1 = reactive<{ value: number }>(obj); console.log("-------------------------------------"); console.log(obj === reactiveObj); // false console.log(reactiveObj === reactiveObj1); // true console.log(obj); console.log(reactiveObj); const addObj = (): void => { obj.value += 1; console.log(obj); console.log("----" + reactiveObj); }; const addRef = (): void => { reactiveObj.value += 1; console.log(obj); console.log("0000"); console.log(reactiveObj); obj = { value: 1000 }; }; function addRef1(): void { reactiveObj1.value += 1; console.log("1111:" + reactiveObj1.value); reactiveObj = { value: 1000 }; } </script>

What is expected?

the result is not the same with playground .
I think the result of playground is OK, but the real project is not the same with playground.

What is actually happening?

I have changed the reactive value to 1000 , but the template's result is not 1000 in the real project created by the command of quick start of document of vue3.


no

@asialiugf
Copy link
Author

asialiugf commented Apr 18, 2022

I have do it followed your sugestion, it did not work yet.

I have changed it to

<scritpt setup lang="ts">

, so I think no need to import defineomponent ,

1= npm init vite@latest :
or npm init vue@latest : select vue ts)
2= copy the playground App.vue to the project's App.vue
3= run it , you can reproduce it

the realworld envirenment of vue is working no correct ...

@liulinboyi
Copy link
Member

liulinboyi commented Apr 18, 2022

Add Example

This is probably a bug.

nomal

<template>
  <div>obj:{{ obj }}</div>
  <div>reactiveObj:{{ reactiveObj }}</div>
  <div>reactiveObj1:{{ reactiveObj1 }}</div>
  <div><button @click="addRef1">修改reactive1</button></div>
</template>

<script lang="ts">
import { reactive, ref, toRefs } from "vue";

export default {
    setup() {
      let obj = {
        value: 0
      };
      let obj1 = {
        value: 0
      };
      let reactiveObj = reactive(obj);
      const reactiveObj1 = reactive(obj);

      console.log("-------------------------------------");
      console.log(obj === reactiveObj); // false
      console.log(reactiveObj === reactiveObj1); // true

      console.log(obj);
      console.log(reactiveObj);

      function addRef1() {
        reactiveObj1.value += 1;
        console.log("1111:" + reactiveObj1.value);
        reactiveObj = { value: 1000 };
        console.log(reactiveObj)
      }

      return {
        obj,
        obj1,
        reactiveObj,
        reactiveObj1,
        addRef1
      }
    }
}
</script>

image

setup

<template>
  <div>obj:{{ obj }}</div>
  <div>reactiveObj:{{ reactiveObj }}</div>
  <div>reactiveObj1:{{ reactiveObj1 }}</div>
  <div><button @click="addRef1">修改reactive1</button></div>
</template>

<script setup lang="ts">
import { reactive, ref, toRefs } from "vue";
import { defineComponent } from "@vue/runtime-core";

let obj = { value: 0 };
let obj1 = { value: 0 };
let reactiveObj = reactive<{ value: number }>(obj);
const reactiveObj1 = reactive<{ value: number }>(obj);

console.log("-------------------------------------");
console.log(obj === reactiveObj); // false
console.log(reactiveObj === reactiveObj1); // true

console.log(obj);
console.log(reactiveObj);

function addRef1(): void {
  reactiveObj1.value += 1;
  console.log("1111:" + reactiveObj1.value);
  reactiveObj = { value: 1000 };
  console.log(reactiveObj)
}
</script>

image

@lidlanca
Copy link
Contributor

Likely duplicate of
#5655

@posva
Copy link
Member

posva commented Apr 18, 2022

Please be clear when reporting a bug, it's quite hard to decipher.

Note you should do const reactiveObj = reactive(...), not let reactiveObj = ... and not reassign the object.

@posva posva closed this as completed Apr 18, 2022
@asialiugf
Copy link
Author

yes

@github-actions github-actions bot locked and limited conversation to collaborators Sep 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants