0

I have a little problem with my code and need some advice.

I try to simulate a diceroll with Vue.js. To be sure any diceroll is different, i want to create a component for that. I use that code for my app.js

Vue.component('diceroll', {
  template: 'This is the result !' + diceroll,
  data: function() {
    return {
      diceroll: 0
    }
  },
  methods: function(){
     diceroll: Math.floor(Math.random() * 6) + 1;
  }
}
)

var demo = new Vue( {

  el: ' #demo',
}
)

Obviously, it don't work and i don't understand how to do that. I read the doc and watch the laracast's series but...

Someone can help me on this ? ^^

1 Answer 1

4

"methods" in Vue are actually objects (key-value pair) where the value is a function. Also, inside the template you have to refer variables using mustache binding like this: {{ vName }}.

I made example: (here is a jsbin demo)

Vue.component('diceroll', {
  template: 'This is the result: {{diceroll}}',
  data: function() {
    return {
      diceroll: 0
    };
  },
  methods: {
    roll: function() {
      this.diceroll = Math.floor(Math.random() * 6) + 1;
    }
  },
  ready: function() {
    this.roll();
  }
});

var demo = new Vue({
  el: '#demo'
});
<script src="http://vuejs.org/js/vue.js"></script>
<div id="demo">
  <diceroll></diceroll>
</div>

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.