1

Mixin data don't seem to be available to initialize Component data.

In the below fiddle, App is using MyMixin. mixinMessage is available in created() hook but not in data initialization. What am I doing wrong?

https://jsfiddle.net/rb81qp5a/2/

const { default: Component, Mixins } = VueClassComponent

@Component()
class MyMixin extends Vue {
  mixinMessage = 'mixin message'
}

@Component({
  template: `
  <div>
    <h1>Message 1: {{ message1 }}</h1>
    <h1>Message 2: {{ message2 }}</h1>
    </div>
  `,
  mixins: [MyMixin]
})
class App extends Vue {
  message1 = this.mixinMessage
  message2 = ''

  created() {
    this.message2 = this.mixinMessage
  }
}

new Vue({
  el: '#app',
  render: h => h(App)
})

Example without vue-class-component: https://codepen.io/anon/pen/PLdmWE

  • Mixin property (init) is empty but expected to be "Mixin property"
  • Mixin property (created) is "Mixin property" as expected
2
  • Have a look at this post — stackoverflow.com/questions/51873087/… Not exactly similar but i think can solve the issue Commented Mar 18, 2019 at 23:08
  • Mixins do work in my code so the problem is not he same. In the post you mention they use the mixin in a method, as I do in the created() method. The point is about using mixins for data init Commented Mar 19, 2019 at 6:59

0

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.