Vue

by tranxuanthang

HTML

<div id="app">
  16 byte ngẫu nhiên được tạo ra:<br> {{ randomData }}<br>
  <button @click="getRandom()">
    Làm mới
  </button>
</div>

Vue

new Vue({
  el: "#app",
  data: {
    randomData: null
  },
  methods: {
    getRandom () {
      // Tạo typed array với 16 byte
      let byte = new Uint8Array(16);
      // Lấp đầy typed array trên với random value
      crypto.getRandomValues(byte);
      // Cập nhật lên Giao diện
      this.randomData = byte.join(", ");
    }
  },
  mounted: function () {
    // Chạy random ngay vào lần đầu
    this.getRandom();
  }
});