[Vuejs]-Vue's reactivity changes data type

0👍

I’m not exactly sure what’s the issue but I tried rewriting almost the same using a different pattern, could you check if this solves your problem? If not hopefully a different approach would get you more clues.

Basically for what I see you will get a 2 numbers array everytime you create a vector, are you expecting it to be of other type?

Here is my rewrite:

vector.js

const vector = (x, y) => ({
    values: [x, y],
    setX(newValue) {
      this.values[0] = newValue
      return this
    },
    setY(newValue) {
      this.values[1] = newValue
      return this
    },
    getX() {
      return this.values[0]
    },
    getY() {
      return this.values[1]
    }
})

export default vector

export const square = [vector(0, 0).values, vector(1,1).values];
<script>
import vector, { square } from "./vector.js";

export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      square,
      vector: vector(1, 2).values,
      title: "Corners",
    };
  },
  mounted() {
    console.log('square', this.square);
    console.log('vector', this.vector);
  },
};
</script>

Leave a comment