"Vuex getters"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(같은 사용자의 중간 판 6개는 보이지 않습니다)
5번째 줄: 5번째 줄:
* getters는 첫 번째 전달인자로 상태(state)를 받는다.
* getters는 첫 번째 전달인자로 상태(state)를 받는다.


<syntaxhighlight lang='html'>
<syntaxhighlight lang='html' run>
<div id="app">
<div id="app">
   <div>{{ doneTodos }}</div>
   <div>{{ doneTodos }}</div>
</div>
</div>


<script src="//unpkg.com/vue"></script>
<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/vuex"></script>
<script src="//unpkg.com/vuex/dist/vuex.min.js"></script>
<script>
<script>
const store = new Vuex.Store({
const store = new Vuex.Store({
39번째 줄: 39번째 줄:
</script>
</script>
</syntaxhighlight>
</syntaxhighlight>
<jsfiddle>x56mazr0</jsfiddle>


==예제 2: doneTodosCount==
==예제 2: doneTodosCount==
<syntaxhighlight lang='html'>
<syntaxhighlight lang='html' run>
<div id="app">
<div id="app">
   <div>{{ doneTodos }}</div>
   <div>{{ doneTodos }}</div>
48번째 줄: 47번째 줄:
</div>
</div>


<script src="//unpkg.com/vue"></script>
<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/vuex"></script>
<script src="//unpkg.com/vuex/dist/vuex.min.js"></script>
<script>
<script>
const store = new Vuex.Store({
const store = new Vuex.Store({
83번째 줄: 82번째 줄:
</script>
</script>
</syntaxhighlight>
</syntaxhighlight>
<jsfiddle>bzqvot5g</jsfiddle>


==예제 3: getTodoById==
==예제 3: getTodoById==
<syntaxhighlight lang='html'>
<syntaxhighlight lang='html' run>
<div id="app">
<div id="app">
   <span>{{ foo }}</span>
   <span>{{ foo }}</span>
</div>
</div>


<script src="//unpkg.com/vue"></script>
<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/vuex"></script>
<script src="//unpkg.com/vuex/dist/vuex.min.js"></script>
<script>
<script>
const store = new Vuex.Store({
const store = new Vuex.Store({
126번째 줄: 124번째 줄:
</script>
</script>
</syntaxhighlight>
</syntaxhighlight>
<jsfiddle>peho5xnt</jsfiddle>


==같이 보기==
==같이 보기==

2021년 4월 20일 (화) 22:44 기준 최신판

1 개요[ | ]

Vuex getters

2 예제 1: doneTodos[ | ]

  • getters는 첫 번째 전달인자로 상태(state)를 받는다.
<div id="app">
  <div>{{ doneTodos }}</div>
</div>

<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/vuex/dist/vuex.min.js"></script>
<script>
const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: 't1', done: true },
      { id: 2, text: 't2', done: false },
      { id: 3, text: 't3', done: true },
      { id: 4, text: 't4', done: true }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    }
  }
})
new Vue({
  el: '#app',
  store,
  computed: {
    doneTodos () {
      return this.$store.getters.doneTodos
    }
  }
})
</script>

3 예제 2: doneTodosCount[ | ]

<div id="app">
  <div>{{ doneTodos }}</div>
  <div>{{ doneTodosCount }}</div>
</div>

<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/vuex/dist/vuex.min.js"></script>
<script>
const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: 't1', done: true },
      { id: 2, text: 't2', done: false },
      { id: 3, text: 't3', done: true },
      { id: 4, text: 't4', done: true }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    },
    doneTodosCount: (state, getters) => {
      return getters.doneTodos.length
    }
  }
})
new Vue({
  el: '#app',
  store,
  computed: {
    doneTodos () {
      return this.$store.getters.doneTodos
    },
    doneTodosCount () {
      return this.$store.getters.doneTodosCount
    }
  }
})
</script>

4 예제 3: getTodoById[ | ]

<div id="app">
  <span>{{ foo }}</span>
</div>

<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/vuex/dist/vuex.min.js"></script>
<script>
const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: 't1', done: true },
      { id: 2, text: 't2', done: false },
      { id: 3, text: 't3', done: true },
      { id: 4, text: 't4', done: true }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    },
    doneTodosCount: (state, getters) => {
      return getters.doneTodos.length
    },
    getTodoById: (state) => (id) => {
      return state.todos.find(todo => todo.id === id)
    }
  }
})
new Vue({
  el: '#app',
  store,
  computed: {
    foo () {
      return this.$store.getters.getTodoById(2)
    }
  }
})
</script>

5 같이 보기[ | ]

6 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}