Thiết lập vuex, sử dụng state và getters trong vuex(P7)

Ở bài trước mình đã giới thiệu sơ qua về vuex sang bài này mình sẽ đi chi tiết về cách dùng của state và getters Thiết lập sử dụng vuex Các bạn có thể xem chi tiêt tại đây Mình hướng dẫn thiết lập bằng npm, các bạn mở project chạy lệnh sau để

Ở bài trước mình đã giới thiệu sơ qua về vuex sang bài này mình sẽ đi chi tiết về cách dùng của state và getters

Thiết lập sử dụng vuex

Các bạn có thể xem chi tiêt tại đây
Mình hướng dẫn thiết lập bằng npm, các bạn mở project chạy lệnh sau để cài đặt vuex

npm install vuex --save

Sau khi cài đặt ta tạo 1 folder store để lưu trữ

  • Thiết lập file index.js cho 1 module
file index.js cơ bản
import Vue from"vue";import VueX from"vuex";...
Vue.use(VueX);const storeData ={
  state:{},
  getters:{},
  mutations:{},
  actions:{},};const store =newVueX.Store(storeData);exportdefault store;

Ta khai báo: state, getters, muations, actions trong storeData, các trạng thái và data sử dụng ta có thể định nghĩa trong từng phần tương ứng

  • Thiết lập file index.js khi có nhiều modules
    Khi có nhiều modules thiết lập thì chúng ta dùng như sau
import Vue from"vue";import VueX from"vuex";
Vue.use(VueX);const moduleUser ={
  state:{},
  mutations:{},
  actions:{},
  getters:{}};const modulePost ={
  state:{},
  mutations:{},
  actions:{}};const storeData ={
  modules:{
    posts: modulePost,
    users: moduleUser
  }};const store =newVueX.Store(storeData);exportdefault store;

store.state.posts // -> `modulePost`'s state
store.state.users // -> `moduleUser`'s state

Chúng ta cũng có thể tách file ra cho gọn gàng dễ nhìn
index.js

import Vue from"vue";import VueX from"vuex";// import modulesimport auth from"./modules/users";import todos from"./modules/posts";

Vue.use(VueX);const storeData ={
  modules:{
    users,
    posts
  },};const store =newVueX.Store(storeData);exportdefault store;

tạo folder modules để lưu các module xử lý
posts.js

import axios from"axios";const postModule ={
  state:{
    posts:[]},
  getters:{},
  actions:{},
  mutations:{}};exportdefault postModule;

State

  • Ví dụ ta có 1 danh sách cách bài viết tĩnh muốn hiển thị lên như sau
    Thiết lập data bài viết trong state
    po
 state:{
    posts:[{
        id:1,
        title:"Bài viết số 1"},{
        id:2,
        title:"Bài viết số 2"},{
        id:3,
        title:"Bài viết số 3"},{
        id:4,
        title:"Bài viết số 4"}]},

Lấy danh sách bài viết ra render ở component
Post.vue

<template>
  <div>
    <h1>Danh sách bài viết</h1>
    <ul>
      <li v-for="post in posts" :key="post.id">{{ post.title }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "list-post",
  computed: {
    posts() {
      return this.$store.state.posts.posts;
    }
  }
};
</script>
<style scoped></style>

Vậy ta đã lấy được danh sách bài viết dựa vào store và state

Getters

  • Ta có cách khác để lấy được data posts dùng getters để trả về data
    posts.js
 getters:{posts:state=> state.posts
  },

Post.vue thay vào computed:

posts(){returnthis.$store.getters.posts;}

Ta có thể gọi getters ngắn gọn như sau

import{ mapGetters }from"vuex";exportdefault{
  name:"list-post",
  computed:{...mapGetters(["posts"]),// posts() {//   return this.$store.getters.posts;// }}};

Như vậy mình đã giới thiệu cho mọi người thiết lập vuex cách dùng state và getters bạn cần luyện tập nhiều hơn để hiểu rõ hơn. Bài sau mình sẽ giới thiệu cho các bạn về mutations và actions cách lấy data động

Nguồn: viblo.asia

Bài viết liên quan

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=

Bài 1 – React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an t

Chuyển đổi từ monolith sang microservices qua ví dụ

1. Why microservices? Microservices là kiến trúc hệ thống phần mềm hướng dịch vụ,