kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Vue.js の基礎を学ぶために LearnCode.academy の学習動画を活用したら最高だった

LearnCode.academy とは?


www.youtube.com

LearnCode.academy  YouTube Vue.js  React + Redux Vue Tutorial91Vue.js 


Vue Tutorial #1 - Vue JS Tutorial for Beginners #1 setting up an app

Vue Tutorial #2 - Vue.js filters and computed data

Vue Tutorial #3 - Vue.js directives and event listeners

Vue Tutorial #4 - Break up your app into Vue Components

Vue Tutorial #5 - AJAX data and Lifecycle Methods

Vue Tutorial #6 - Full REST Example

Vue Tutorial #7 - Scaling Vue with Single File Components

Vue Tutorial #8 - Vue Router

Vue Tutorial #9 - Scaling Vue.js Data with Stores


 GitHub  Vue.js 

github.com

Vue Tutorial #1


 index.html  div 
<div id="app"></div>

Vue 


el ... 

data ... 

template ... 


Vue.js  Mustache  {{john.name}} 
const app = new Vue({
  el: "#app",
  data: {
    bobby: {
      name: "Bobby Boone",
      age: 25
    },
    john: {
      name: "John Boby",
      age: 35,
    }
  },
  template: `
    <div>
      <h2>Name: {{john.name}}</h2>
      <h2>Age: {{john.age}}</h2>
      <h2>Name: {{bobby.name}}</h2>
      <h2>Age: {{bobby.age}}</h2>
    </div>
  `
})

www.youtube.com

Vue Tutorial #2


computed 使 {{bobbyFullName}} 
const app = new Vue({
  (中略)
  computed: {
    bobbyFullName() {
      return `${this.bobby.first} ${this.bobby.last}`
    },
    johnFullName() {
      return `${this.john.first} ${this.john.last}`
    }
  },
  (中略)
})

filters 使 {{bobby | fullName}}  bobby  fullName 
const app = new Vue({
  el: "#app",
  data: {
    bobby: {
      first: "Bobby",
      last: "Boone",
      age: 25
    },
    john: {
      first: "John",
      last: "Boone",
      age: 35,
    }
  },
  computed: {
    johnAgeInOneYear() {
      return this.john.age + 1;
    }
  },
  filters: {
    ageInOneYear(age) {
      return age + 1;
    },
    fullName(value) {
      return `${value.last}, ${value.first}`;
    }
  },
  template: `
    <div>
      <h2>Name: {{john | fullName}}</h2>
      <h2>Age: {{john.age | ageInOneYear}}</h2>
      <h2>Name: {{bobby | fullName}}</h2>
      <h2>Age: {{bobby.age | ageInOneYear}}</h2>
    </div>
  `
})

www.youtube.com

Vue Tutorial #3


 v-for 使
<div>
  <h2 v-for="friend in friends">{{friend | fullName}}</h2>
</div>

 v-model 使v-model  v-bind:value  v-on:input 
<div>
  <h2 v-for="friend in friends">
    <h4>{{friend | fullName}}</h4>
    <input v-model="friend.first"/>
    <input v-model="friend.last"/>
  </h2>
</div>

 v-on:click  computed  methods  computed 使

vuejs.org
const app = new Vue({
  (中略)
  methods: {
    decrementAge(friend) {
      friend.age = friend.age - 1;
    },
    incrementAge(friend) {
      friend.age = friend.age + 1;
    },
  },
  template: `
    <div>
      <h2 v-for="friend in friends">
        <h4>{{friend | fullName}}</h4>
        <h5>age: {{friend.age}}</h5>
        <button v-on:click="decrementAge(friend)">-</button>
        <button v-on:click="incrementAge(friend)">+</button>
        <input v-model="friend.first"/>
        <input v-model="friend.last"/>
      </h2>
    </div>
  `
})

www.youtube.com

Vue Tutorial #4


Vue 使Vue Vue  props 使Vue.js Vue 
Vue.component('friend-component', {
  props: ['friend'],
  filters: {
    ageInOneYear(age) {
      return age + 1;
    },
    fullName(value) {
      return `${value.last}, ${value.first}`;
    }
  },
  methods: {
    decrementAge(friend) {
      friend.age = friend.age - 1;
    },
    incrementAge(friend) {
      friend.age = friend.age + 1;
    },
  },
  template: `
    <div>
      <h4>{{friend | fullName}}</h4>
      <h5>age: {{friend.age}}</h5>
      <button v-on:click="decrementAge(friend)">-</button>
      <button v-on:click="incrementAge(friend)">+</button>
      <input v-model="friend.first"/>
      <input v-model="friend.last"/>
    </div>
  `
});

const app = new Vue({
  (中略)
  template: `
    <div>
      <friend-component v-for="item in friends" v-bind:friend="item" />
    </div>
  `
})

www.youtube.com

Vue Tutorial #4 

f:id:kakku22:20180218050849p:plain

Vue Tutorial #5 & Vue Tutorial #6


Vue  el app.$mount("#app") Vue  beforeMount  mounted  fetch() 使 API 

v-on:click  v-on:keyup.13 使 Enter v-if  v-else 使
const app = new Vue({
    el: "#app",
    data: {
      editFriend: null,
      friends: [],
    },
    methods: {
      deleteFriend(id, i) {
        fetch("http://rest.learncode.academy/api/vue-5/friends/" + id, {
          method: "DELETE"
        })
        .then(() => {
          this.friends.splice(i, 1);
        })
      },
      updateFriend(friend) {
        fetch("http://rest.learncode.academy/api/vue-5/friends/" + friend.id, {
          body: JSON.stringify(friend),
          method: "PUT",
          headers: {
            "Content-Type": "application/json",
          },
        })
        .then(() => {
          this.editFriend = null;
        })
      }
    },
    mounted() {
      fetch("http://rest.learncode.academy/api/vue-5/friends")
        .then(response => response.json())
        .then((data) => {
          this.friends = data;
        })
    },
    template: `
    <div>
      <li v-for="friend, i in friends">
        <div v-if="editFriend === friend.id">
          <input v-on:keyup.13="updateFriend(friend)" v-model="friend.name" />
          <button v-on:click="updateFriend(friend)">save</button>
        </div>
        <div v-else>
          <button v-on:click="editFriend = friend.id">edit</button>
          <button v-on:click="deleteFriend(friend.id, i)">x</button>
          {{friend.name}}
        </div>
      </li>
    </div>
    `,
});

www.youtube.com

www.youtube.com

Vue Tutorial #7


vue-cli + webpack  Vue.js  Tutorial 4  Vue  .vue Vue  <style scoped> 

vuejs.org

 App.vue  GitHub 
<template>
  <div id="app">
    <img src="./assets/logo.png">
    <HelloWorld/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld'

export default {
  name: 'app',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

www.youtube.com

Vue Tutorial #8


 Vue Router 使 SPA SPA URL  # router/index.js 
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Account from '@/components/Account'
import Contact from '@/components/Contact'
import Friends from '@/components/Friends'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Hello',
      component: HelloWorld
    },
    {
      path: '/friends/:id/:age/:weight',
      name: 'Friends',
      props: true,
      component: Friends
    },
    {
      path: '/contact',
      name: 'Contact',
      component: Contact
    },
    {
      path: '/account',
      name: 'Account',
      component: Account
    }
  ]
})

Vue  {{$route.params.id}}  props 
<template>
  <div>
    <h1>Friends</h1>
    {{id}}
    {{age}}
    {{weight}}
  </div>
</template>

<script>
export default {  
  props: [
    'id',
    'age',
    'weight',
  ]

}
</script>

www.youtube.com

Vue Tutorial #8 

f:id:kakku22:20180218050925p:plain

Vue Tutorial #9


 Vue.js  Store  stores/FriendStore.js  Store  Tutorial Flux / vuex 
const FriendStore = {
  data: {
    friends: ["bobby", "billy"],
  },
  methods: {
    addFriend(name) {
      FriendStore.data.friends.push(name);
    }
  }
};

export default FriendStore;

 Vue  Store 
<template>
  <div>
    <h1>My Friends</h1>
    <li v-for="friend in FriendStore.friends">{{friend}}</li>
    <input v-model="newFriend" v-on:keyup.13="addFriend(newFriend)" />
  </div>
</template>

<script>
import FriendStore from "../stores/FriendStore"

export default {
  data() {
    return {
      newFriend: null,
      FriendStore: FriendStore.data,
    };
  },
  methods: {
    addFriend(name) {
      FriendStore.methods.addFriend(name)
      this.newFriend = null;
    }
  }
}
</script>

www.youtube.com

Vue Tutorial #9 

f:id:kakku22:20180218050939p:plain



LearnCode.academy Vue Tutorial Vue.js 

Vue.js 

MVVM 便 Vue Vue.js 



Introduction  Vue.js
  Vue.js



 Vue.js 使VS Code + Vetur  Chrome  vue-devtools 使

github.com

github.com


jQuery  SPA 

kakakakakku.hatenablog.com