File tree Expand file tree Collapse file tree 11 files changed +100
-9
lines changed Expand file tree Collapse file tree 11 files changed +100
-9
lines changed Original file line number Diff line number Diff line change 1+ ** VueJS Typescript with Vuex support using the Vue CLI 3**
2+
3+ Made from this great tutorial: https://codeburst.io/vuex-and-typescript-3427ba78cfa8
Original file line number Diff line number Diff line change 1919 },
2020 "devDependencies" : {
2121 "@types/jest" : " ^22.0.1" ,
22- "@vue/cli-plugin-babel" : " ^3.0.0-beta.6 " ,
23- "@vue/cli-plugin-typescript" : " ^3.0.0-beta.6 " ,
24- "@vue/cli-plugin-unit-jest" : " ^3.0.0-beta.6 " ,
25- "@vue/cli-service" : " ^3.0.0-beta.6 " ,
22+ "@vue/cli-plugin-babel" : " ^3.0.0-beta.7 " ,
23+ "@vue/cli-plugin-typescript" : " ^3.0.0-beta.7 " ,
24+ "@vue/cli-plugin-unit-jest" : " ^3.0.0-beta.7 " ,
25+ "@vue/cli-service" : " ^3.0.0-beta.7 " ,
2626 "@vue/test-utils" : " ^1.0.0-beta.10" ,
2727 "babel-core" : " ^7.0.0-0" ,
2828 "node-sass" : " ^4.7.2" ,
Original file line number Diff line number Diff line change 2222
2323 @Component
2424 export default class HelloWorld extends Vue {
25+ @State (' profile' ) profile: ProfileState ;
26+ @Action (' fetchData' , {namespace }) fetchData: any ;
27+ @Getter (' fullName' , {namespace }) fullName: string ;
2528
2629 @Prop () private msg! : string ;
2730
2831 constructor () {
2932 super ();
3033 }
3134
32- @State (' profile' ) profile: ProfileState ;
33- @Action (' fetchData' , {namespace }) fetchData: any ;
34- @Getter (' fullName' , {namespace }) fullName: string ;
35-
3635 mounted() {
3736 // fetching data as soon as the component's been mounted
3837 this .fetchData ();
Original file line number Diff line number Diff line change 1+
2+ import { ActionTree } from 'vuex' ;
3+ import axios from 'axios' ;
4+ import { ProfileState , User } from './types' ;
5+ import { RootState } from '../types' ;
6+
7+
8+ export const actions : ActionTree < ProfileState , RootState > = {
9+ fetchData ( { commit } ) : any {
10+ axios ( {
11+ url : 'https://next.json-generator.com/api/json/get/41AeRPqnE' ,
12+ } ) . then ( ( response ) => {
13+ const payload : User = response && response . data ;
14+ commit ( 'profileLoaded' , payload ) ;
15+ } , ( error ) => {
16+ console . log ( error ) ;
17+ commit ( 'profileError' ) ;
18+ } ) ;
19+ } ,
20+ } ;
Original file line number Diff line number Diff line change 1+ import { GetterTree } from 'vuex' ;
2+ import { ProfileState } from './types' ;
3+ import { RootState } from '../types' ;
4+
5+ export const getters : GetterTree < ProfileState , RootState > = {
6+ fullName ( state ) : string {
7+ const { user } = state ;
8+ console . log ( 'user' , user ) ;
9+ const firstName = ( user && user . firstName ) || '' ;
10+ const lastName = ( user && user . lastName ) || '' ;
11+ return `${ firstName } ${ lastName } ` ;
12+ } ,
13+ } ;
Original file line number Diff line number Diff line change 1+ import { Module } from 'vuex' ;
2+ import { getters } from './getters' ;
3+ import { actions } from './actions' ;
4+ import { mutations } from './mutations' ;
5+ import { ProfileState } from './types' ;
6+ import { RootState } from '../types' ;
7+
8+ export const state : ProfileState = {
9+ user : undefined ,
10+ error : false ,
11+ } ;
12+
13+ const namespaced : boolean = true ;
14+
15+ export const profile : Module < ProfileState , RootState > = {
16+ namespaced,
17+ state,
18+ getters,
19+ actions,
20+ mutations,
21+ } ;
Original file line number Diff line number Diff line change 1+ import { MutationTree } from 'vuex' ;
2+ import { ProfileState , User } from './types' ;
3+
4+ export const mutations : MutationTree < ProfileState > = {
5+ profileLoaded ( state , payload : User ) {
6+ state . error = false ;
7+ state . user = payload ;
8+ } ,
9+ profileError ( state ) {
10+ state . error = true ;
11+ state . user = undefined ;
12+ } ,
13+ } ;
Original file line number Diff line number Diff line change 1+
2+ export interface User {
3+ firstName : string ;
4+ lastName : string ;
5+ email : string ;
6+ phone ?: string ;
7+ }
8+
9+ export interface ProfileState {
10+ user ?: User ;
11+ error : boolean ;
12+ }
Original file line number Diff line number Diff line change 1+ export interface RootState {
2+ version : string ;
3+ }
Original file line number Diff line number Diff line change 1010 "ordered-imports" : false ,
1111 "object-literal-sort-keys" : false ,
1212 "no-consecutive-blank-lines" : false ,
13- "no-console" : [false ]
13+ "no-console" : [false ],
14+ "member-access" : [true , " no-public" ]
1415 }
1516}
You can’t perform that action at this time.
0 commit comments