2

Demo: https://codesandbox.io/s/23959y5wnp

So I'm passing down a function and would like to rebind the this so I used .bind(this) on the function, yet the data returned is still based on the original component. What am I missing?

Expected: Test2 should print out Test2 on button click

Code:

App.vue

<template>
  <div id="app">
    <img width="25%" src="./assets/logo.png" /><br />
    <Test1 :aFunction="passThis" /> <Test2 :aFunction="dontPassThis" />
  </div>
</template>

<script>
import Test1 from "./components/Test1";
import Test2 from "./components/Test2";

export default {
  name: "App",
  components: {
    Test1,
    Test2
  },
  data() {
    return {
      value: "original"
    };
  },
  methods: {
    dontPassThis($_) {
      console.log(this.value);
    },
    passThis($_) {
      console.log($_.value);
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Test1.vue

<template>
  <div>Test1 <button @click="() => aFunction(this)">click me</button></div>
</template>

<script>
export default {
  data() {
    return {
      value: "Test1"
    };
  },
  mounted() {
    this.aFunction(this);
  },
  props: {
    aFunction: {
      required: true,
      type: Function
    }
  }
};
</script>

Test2.vue

<template>
  <div>
    Test2

    <button @click="testFunction">click me</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      testFunction: null,
      value: "Test2"
    };
  },
  created() {
    this.testFunction = this.aFunction.bind(this);
  },
  props: {
    aFunction: {
      required: true,
      type: Function
    }
  }
};
</script>
8
  • testFunction is not a property of the exported object. testFunction is an object returned from the data function within the exported object.. Commented Feb 24, 2019 at 17:37
  • @guest271314 for Test2.vue I've also tried changing the @click to () => aFunction.bind(this)(). But it still prints out "original" Commented Feb 24, 2019 at 17:40
  • Have not tried vue.js. Where is the data function called? Commented Feb 24, 2019 at 17:42
  • @guest271314 that data function just sets up the component so that the variables inside the returned object are accessible via the this.X Commented Feb 24, 2019 at 17:44
  • Why are there two different exported objects that have a property with the same name "data"? Commented Feb 24, 2019 at 17:47

1 Answer 1

6

Vue already binds the component's methods during initialization, and functions cannot be bound more than once (subsequent binds have no effect).

So when App is initialized, Vue binds the App instance as the context of its dontPassThis method. App "passes" dontPassThis to Test2 via a prop, which Test2 subsequently tries to bind, which actually does nothing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.