• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Vuex]UseNamespacesinVuexStoresusingTypeScript

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Even by using modules, they still share the same namespace. So you couldn’t have the same mutation name in different modules. Namespaces solve that by prepending the path of the module to the StateGetterAction, and Mutation.

This lesson shows how to use namespaces in Vuex modules with TypeScript.

 

Enable namespaced to each feature store:

import {GetterTree, MutationTree, Module} from 'vuex';
import {TodosState, RootState} from '../types';
import {Todo} from '../types';

const state: TodosState = {
    todos: [
        {text: 'Buy milk', checked: false},
        {text: 'Learning', checked: true},
        {text: 'Algorithom', checked: false},
    ],
};

const getters: GetterTree<TodosState, RootState> = {
    todos: (state, getters, rootState) => state.todos.filter(t => !t.checked),
    dones: (state, getters, rootState) => state.todos.filter(t => t.checked)
};

const mutations: MutationTree<TodosState> = {
    addTodo(state, newTodo) {
        const copy = {
            ...newTodo
        };
        state.todos.push(copy);
    },
    toggleTodo(TodosState, todo) {
        todo.checked = !todo.checked;
    }
};

const actions: ActionTree<TodosState, RootState> = {
    addTodoAsync(context, id) {
        fetch('https://jsonplaceholder.typicode.com/posts/'+id)
            .then(data => data.json())
            .then(item => {
                const todo: Todo = {
                    checked: false,
                    text: context.rootState.login.user + ': ' + item.title
                }

                context.commit('addTodo', todo);
            })
    }
}

export const todos: Module<TodosState, RootState> = {
    actions,
    state,
    mutations,
    getters,
    namespaced: true
};

 

Now we need to modify the component to adopt the namespace:

<template>
<section>
    <h4>Todos</h4>
    <ul>
      <li v-for="todo in todos">
        <input type="checkbox" :checked="todo.checked" @change="toggleTodo(todo)">
        {{ todo.text }}
      </li>
    </ul>
    <h4>Done</h4>
    <ul>
      <li v-for="todo in dones">
        <input type="checkbox" :checked="todo.checked" @change="toggleTodo(todo)">
        {{ todo.text }}
      </li>
    </ul>
    <p>
      <label for="">
        Add todo:
        <input type="text" v-model="newTodo.text" @keyup.enter="addTodo(newTodo)">
      </label>
      <label for="">
        Add todo async:
        <input type="text" v-model="id" @keyup.enter="addTodoAsync(id)">
      </label>

    </p>
</section>
</template>

<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import {Action, State, Getter, Mutation, namespace} from 'vuex-class';
import {TodosState, Todo} from '../types';

const TodoGetter = namespace('todos', Getter);
const TodoAction = namespace('todos', Action);
const TodoMutation = namespace('todos', Mutation);

@Component({
})
export default class Todos extends Vue {
    @TodoGetter todos: TodosState;
    @TodoGetter dones: TodosState;

    @TodoMutation addTodo;
    @TodoMutation toggleTodo;
    @TodoAction addTodoAsync;

    newTodo: Todo = {
      text: '',
      checked: false
    }

    id: string = '1';
}
</script>

 

If we want to trigger a different namesapce state update from Todo State, we can do it in action:

const actions: ActionTree<TodosState, RootState> = {
    addTodoAsync(context, id) {
        fetch('https://jsonplaceholder.typicode.com/posts/'+id)
            .then(data => data.json())
            .then(item => {
                const todo: Todo = {
                    checked: false,
                    text: context.rootState.login.user + ': ' + item.title
                }

                context.commit('addTodo', todo);
                context.commit('login/login', null, {root: true}); // we have to say {root: true}, otherwise, it will look for the state under 'todos' namespace
            })
    }
}

Login mutation:

const mutations: MutationTree<LoginState> = {
    login (state) {
        state.isLoggedIn = true;
        state.user =  'alice';
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
在 Typescript 2.0 中使用 @types 类型定义发布时间:2022-07-18
下一篇:
TypeScript:类(Classes)发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap