LoginSignup
1
1

More than 3 years have passed since last update.

axiosとvue.jsで簡易な掲示板を作る

Posted at

コードの中のコメント参考に

ボタンを押したら、
postメソッドでデータの登録を行います。
そして、からの配列に入れて、表示するというものです。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>axiosとvue.jsによる掲示板</title>
</head>
<body>
  <div id="app">
    <input v-model="name"><br>
    <button v-on:click="createNewUser">作成</button>
    <ul>
      <li v-for="user in users">{{ user.name }}</li>
    </ul>
  </div>

  <script src="https://unpkg.com/vue@next"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

  <script>
Vue.createApp({
    el: '#app',
    data(){
        return {
            users: [],
        }
    },
    methods : {
    createNewUser: function(){
     axios.post('https://jsonplaceholder.typicode.com/users',{
            name: this.name
           })
          .then(response => this.users.unshift(response.data))
          .catch(error => console.log(error))     
    },
  },

}).mount('#app')
</script>

</body>
</html>

参考
https://reffect.co.jp/vue/vue-axios-learn

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