1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Realtime Database + Vue.js TODOアプリ

Last updated at Posted at 2020-05-05
  • 作るもの
    realtimedb.png

  • Realtime Databaseの設定例
    db.png

vue.js

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <meta charset="UTF-8">
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="text1">
      <input type="button" @click="put()" value="PUT">
      <table border="1" v-if="items">
        <tr v-for="(item, i) in items">
          <td>{{ item.name }}</td>
          <td><input type="button" @click="del(i)" value="DELETE"></td>
          <td><input type="button" @click="update(i)" value="PUT"></td>
        </tr>
      </table>
    </div>
  </body>
</html>

<script>

const baseUrl ='https://test-0000.firebaseio.com/'; // Realtime DatabaseのURLを設定

const app = new Vue({
  el: '#app',
  data: {
    text1: '',
    items: []
  },
  mounted() {
    this.find();
  },
  methods: {
    async find() {
      const url = baseUrl + '.json';
      const res = await axios.get(url);
      this.items = res.data;
      console.log(111, res.data);
    },
    async put() {
      const url = baseUrl + Date.now() + '.json';
      const params = { name: this.text1 };
      const res = await axios.put(url, params);
      this.find();
    },
    async update(i) {
      const url = baseUrl + i + '.json';
      const params = { name: this.text1 };
      const res = await axios.put(url, params);
      this.find();
    },
    async del(i) {
      const url = baseUrl + i + '.json';
      const res = await axios.delete(url);
      this.find();
    },
  }
})
</script>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?