Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vue.jsでコンポーネント入門!ハンズオン勉強会

naokie
July 05, 2016

 Vue.jsでコンポーネント入門!ハンズオン勉強会

https://itstaffing.connpass.com/event/34159/
Vue.jsでコンポーネント入門!ハンズオン勉強会

naokie

July 05, 2016
Tweet

More Decks by naokie

Other Decks in Technology

Transcript

  1. ߏ੒͢ΔϦιʔε 1. Custom Elements Ϣʔβʔ͕ಠࣗͷΧελϜཁૉΛఆٛ 2. HTML Imports அยԽͨ͠HTMLϑΝΠϧΛϩʔυ 3.

    HTML Template ϒϥ΢βωΠςΟϒͳςϯϓϨʔτػೳ 4. Shadow DOM HTMLʹείʔϓΛܗ੒
  2. Vol.1 ςϯϓϨʔτ <div id="my-app"> <!-- ৽نλεΫͷ௥Ճ --> <p> NewTask: <input

    type="text" v-model="newTask" /> <button v-on:click="addTodo">Add</button> </p> <hr /> <!-- λεΫͷϦετදࣔ --> <ul> <li v-for="todo in todos"> <input type="checkbox" v-model="todo.isCompleted" /> <span v-bind:class="{ 'complete': todo.isCompleted }">{{ todo.task }}</span> <button v-on:click="deleteTodo(todo)">Delete</button> </li> </ul> <!-- ࢒ΓλεΫͷΧ΢ϯτදࣔ --> <p>Remaining Tasks: {{ remains }}/{{ todos.length }}</p> </div>
  3. Vol.1 Vue Πϯελϯε var vm = new Vue({ el: '#my-app',

    // Ϛ΢ϯτ͢Δཁૉ data: { // ϓϩΩγ͢Δ͢΂ͯͷϓϩύςΟ newTask: '', todos: [ { task: 'ڇೕΛങ͏', isCompleted: false }, { task: 'ϓϩςΠϯΛങ͏', isCompleted: true }, { task: 'εϙʔπυϦϯΫΛങ͏', isCompleted: false } ] }, computed: { // ࢉग़ϓϩύςΟ remains: function() { var inCompleteList = this.todos.filter(function(task) { return !task.isCompleted; }); return inCompleteList.length; } }, methods: { // Πϯελϯεϝιου addTodo: function() { if (this.newTask == '') return; this.todos.push( { task: this.newTask, isCompleted: false } ); this.newTask = ''; }, deleteTodo: function(todo) { this.todos.$remove(todo); } } });
  4. HTML Template Λ੾Γग़͠ <script type="text/template" id="t_todo"> <li> <input type="checkbox" v-model="todo.isCompleted"

    /> <span v-bind:class="{ 'complete': todo.isCompleted }">{{ todo.task }}</span> <button v-on:click="deleteTodo(todo)">Delete</button> </li> </script>
  5. ਌ͰΠϕϯτΛϦοεϯ var vm = new Vue({ ... events: { 'delete':

    function(todo) { this.todos.$remove(todo); } } });
  6. HTML Template Λ੾Γग़͠ <script type="text/template" id="t_app"> <!-- divཁૉͰғΉ --> <div>

    <p> NewTask: <input type="text" v-model="newTask" /> <button v-on:click="addTodo">Add</button> </p> <hr /> <ul> <todo v-for="todo in todos" :todo="todo"></todo> </ul> <p>Remaining Tasks: {{ remains }}/{{ todos.length }}</p> </div> </script>
  7. ίϯϙʔωϯτͷ࡞੒ var App = Vue.extend({ template: '#t_app', props: { 'todos':

    { required: true } }, ... components: { Todo: Todo } });
  8. App ίϯϙʔωϯτ͔ΒΠϕϯτΛૹग़ var App = Vue.extend({ // ίϯϙʔωϯτఆٛͰ͸ॳظσʔλΦϒδΣΫτΛฦؔ͢਺ͱͯ͠એݴ͢Δ data: function()

    { return { newTask: '', }; }, ... methods: { addTodo: function() { if (this.newTask == '') return; this.$dispatch('add', this.newTask); this.newTask = ''; } } });
  9. ਌ͰΠϕϯτΛϦοεϯ var vm = new Vue({ ... events: { ...

    'add': function(newTask) { this.todos.push( { task: newTask, isCompleted: false } ); } } });
  10. ࢒ΓλεΫͷΧ΢ϯτදࣔ var App = Vue.extend({ ... computed: { remains: function()

    { var inCompleteList = this.todos.filter(function(task) { return !task.isCompleted; }); return inCompleteList.length; } }, ... });
  11. ίϯϙʔωϯτԽ var NewTask = Vue.extend({ template: '#t_newtask', data: function() {

    return { newTask: '', }; }, methods: { addTodo: function() { if (this.newTask == '') return; this.$dispatch('add', this.newTask) this.newTask = ''; } } });