mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Add more realistic TypeScript example
This commit is contained in:
parent
9c754f8021
commit
44c0ab78a2
@ -12,11 +12,15 @@
|
||||
import Vue from 'vue'
|
||||
import Component from 'nuxt-class-component'
|
||||
import { Prop } from 'vue-property-decorator'
|
||||
import { Action } from 'vuex-class'
|
||||
import { Action, namespace } from 'vuex-class'
|
||||
|
||||
import * as people from '~/store/modules/people';
|
||||
|
||||
const PeopleAction = namespace(people.name, Action);
|
||||
|
||||
@Component({})
|
||||
export default class Card extends Vue {
|
||||
@Prop() person
|
||||
@Action select
|
||||
@PeopleAction select
|
||||
}
|
||||
</script>
|
@ -16,8 +16,13 @@
|
||||
<script lang="ts">
|
||||
import Vue from 'vue'
|
||||
import Component from 'nuxt-class-component'
|
||||
import Card from '~/components/Card'
|
||||
import { State, Getter } from 'vuex-class'
|
||||
import Card from '~/components/Card.vue'
|
||||
import { State, Getter, namespace } from 'vuex-class'
|
||||
|
||||
import * as people from '~/store/modules/people';
|
||||
|
||||
const PeopleState = namespace(people.name, State);
|
||||
const PeopleGetter = namespace(people.name, Getter);
|
||||
|
||||
@Component({
|
||||
components: {
|
||||
@ -25,8 +30,8 @@ import { State, Getter } from 'vuex-class'
|
||||
}
|
||||
})
|
||||
export default class extends Vue {
|
||||
@State selected
|
||||
@State people
|
||||
@Getter selectedPerson
|
||||
@PeopleState selected
|
||||
@PeopleState people
|
||||
@PeopleGetter selectedPerson
|
||||
}
|
||||
</script>
|
||||
|
@ -1,33 +1,25 @@
|
||||
import axios from "~/plugins/axios";
|
||||
import * as root from "./root";
|
||||
import * as people from "./modules/people";
|
||||
|
||||
export const state = () => ({
|
||||
selected: 1,
|
||||
people: []
|
||||
});
|
||||
// more info about store: https://vuex.vuejs.org/en/core-concepts.html
|
||||
// structure of the store:
|
||||
// types: Types that represent the keys of the mutations to commit
|
||||
// state: The information of our app, we can get or update it.
|
||||
// getters: Get complex information from state
|
||||
// action: Sync or async operations that commit mutations
|
||||
// mutations: Modify the state
|
||||
|
||||
export const mutations = {
|
||||
select(state, id) {
|
||||
state.selected = id;
|
||||
},
|
||||
setPeople(state, people) {
|
||||
state.people = people;
|
||||
}
|
||||
export const modules = {
|
||||
[people.name]: people
|
||||
};
|
||||
|
||||
export const getters = {
|
||||
selectedPerson: state => {
|
||||
const p = state.people.find(person => person.id === state.selected);
|
||||
return p ? p : { first_name: "Please,", last_name: "select someone" };
|
||||
}
|
||||
};
|
||||
interface ModulesStates {
|
||||
people: people.State;
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
async nuxtServerInit({ commit }) {
|
||||
const response = await axios.get("/random-data.json");
|
||||
const people = response.data.slice(0, 10);
|
||||
commit("setPeople", people);
|
||||
},
|
||||
select({ commit }, id) {
|
||||
commit("select", id);
|
||||
}
|
||||
};
|
||||
export type RootState = root.State & ModulesStates;
|
||||
|
||||
export const state = root.state;
|
||||
export const getters = root.getters;
|
||||
export const actions = root.actions;
|
||||
export const mutations = root.mutations;
|
70
examples/typescript/store/modules/people.ts
Normal file
70
examples/typescript/store/modules/people.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import { ActionTree, MutationTree, GetterTree, ActionContext } from "vuex";
|
||||
import { RootState } from "store";
|
||||
|
||||
export const name = "people";
|
||||
|
||||
export const types = {
|
||||
SELECT: "SELECT",
|
||||
SET: "SET"
|
||||
};
|
||||
|
||||
export interface PersonContact {
|
||||
email: string;
|
||||
phone: string;
|
||||
}
|
||||
|
||||
export interface PersonAddress {
|
||||
city: string;
|
||||
country: string;
|
||||
postalCode: string;
|
||||
state: string;
|
||||
street: string;
|
||||
}
|
||||
|
||||
export interface Person {
|
||||
id: number;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
contact: PersonContact;
|
||||
gender: string;
|
||||
ip_address: string;
|
||||
avatar: string;
|
||||
addres: PersonAddress;
|
||||
}
|
||||
|
||||
export interface State {
|
||||
selected: number;
|
||||
people: Person[];
|
||||
}
|
||||
|
||||
export const state = (): State => ({
|
||||
selected: 1,
|
||||
people: []
|
||||
});
|
||||
|
||||
export const getters: GetterTree<State, RootState> = {
|
||||
selectedPerson: state => {
|
||||
const p = state.people.find(person => person.id === state.selected);
|
||||
return p ? p : { first_name: "Please,", last_name: "select someone" };
|
||||
}
|
||||
};
|
||||
|
||||
export interface Actions<S, R> extends ActionTree<S, R> {
|
||||
select(context: ActionContext<S, R>, id: number): void;
|
||||
}
|
||||
|
||||
export const actions: Actions<State, RootState> = {
|
||||
select({ commit }, id: number) {
|
||||
commit(types.SELECT, id);
|
||||
}
|
||||
};
|
||||
|
||||
export const mutations: MutationTree<State> = {
|
||||
[types.SELECT](state, id: number) {
|
||||
state.selected = id;
|
||||
},
|
||||
[types.SET](state, people: Person[]) {
|
||||
state.people = people;
|
||||
}
|
||||
};
|
||||
|
28
examples/typescript/store/root.ts
Normal file
28
examples/typescript/store/root.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { GetterTree, ActionContext, ActionTree, MutationTree } from "vuex";
|
||||
import axios from "~/plugins/axios";
|
||||
import { RootState } from "store";
|
||||
import * as people from "./modules/people";
|
||||
|
||||
export const types = {};
|
||||
|
||||
export interface State {}
|
||||
|
||||
export const state = (): State => ({});
|
||||
|
||||
export const getters: GetterTree<State, RootState> = {};
|
||||
|
||||
export interface Actions<S, R> extends ActionTree<S, R> {
|
||||
nuxtServerInit(context: ActionContext<S, R>): void;
|
||||
}
|
||||
|
||||
export const actions: Actions<State, RootState> = {
|
||||
async nuxtServerInit({ commit }) {
|
||||
const response = await axios.get("/random-data.json");
|
||||
const staticPeople = response.data.slice(0, 10);
|
||||
commit(`${people.name}/${people.types.SET}`, staticPeople, { root: true });
|
||||
}
|
||||
};
|
||||
|
||||
export const mutations: MutationTree<State> = {};
|
||||
|
||||
|
@ -23,7 +23,8 @@
|
||||
"~/middleware/*": ["./middleware/*"],
|
||||
"~/pages/*": ["./pages/*"],
|
||||
"~/plugins/*": ["./plugins/*"],
|
||||
"~/static/*": ["./static/*"]
|
||||
"~/static/*": ["./static/*"],
|
||||
"~/store/*": ["./store/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user