Bruce Cantarim
Digital Media Frontend Developer @ArcTouch
Vuex, Vue Router and Typescript
Bruce Cantarim
Digital Media Frontend Developer - Arctouch Brasil
So...
<div id="app">
{{ message }}
</div>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
HTML
JS
So...
$ npm install -g @vue/cli
$ yarn global add @vue/cli
$ vue create project-name
$ cd project-name && vue ui
# or
$ cd project-name && yarn serve
then...
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return “Hello, “ + this.greeting;
};
return Greeter;
})();
JS
class Greeter {
private greeting: string;
constructor (private message: string) {
this.greeting = message;
}
greet() {
return `Hello, ${this.greeting}`;
}
}
TS
Don't be afraid...
$ npm install -S vue-property-decorator
$ yarn add vue-property-decorator
export default {
props: {
propA: {
type: Number
},
propB: {
default: 'default value'
},
propC: {
type: [String, Boolean]
},
}
}
JS
import { Vue, Component, Prop } from 'vue-property-decorator'
@Component
export default class YourComponent extends Vue {
@Prop(Number) readonly propA!: number
@Prop({ default: 'default value' }) readonly propB!: string
@Prop([String, Boolean]) readonly propC!: string | boolean
}
TS
There are 7 decorators and 1 function (Mixin):
$ npm install -S vue-router
$ yarn add vue-router
import Vue from 'vue';
import Router from 'vue-router';
import Home from '@/views/Home.vue';
Vue.use(Router);
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
},
],
});
<template>
<div id="app">
<router-view />
</div>
</template>
...
router.ts
app.vue
$ npm install -S vuex
$ yarn add vuex
import Vue from 'vue';
import Vuex, { StoreOptions } from 'vuex';
import { RootState } from './types';
import { profile } from './profile/index';
Vue.use(Vuex);
const store: StoreOptions<RootState> = {
state: {
version: '1.0.0'
},
modules: {
profile
}
};
export default new Vuex.Store<RootState>(store);
store.ts
export interface RootState {
version: string;
}
types.ts
import { Module } from 'vuex';
import { getters } from './getters';
import { actions } from './actions';
import { mutations } from './mutations';
import { ProfileState } from './types';
import { RootState } from '../types';
export const state: ProfileState = {
user: undefined,
error: false
};
const namespaced: boolean = true;
export const profile: Module<ProfileState, RootState> = {
namespaced,
state,
getters,
actions,
mutations
};
profile/index.ts
Reference:
$ npm install -S vuex-class
$ yarn add vuex-class
import { Vue, Component } from 'vue-property-decorator';
import { State, Getter, Action, Mutation } from 'vuex-class';
@Component
export class MyComp extends Vue {
@State('foo') stateFoo
@Getter('foo') getterFoo
@Action('foo') actionFoo
@Mutation('foo') mutationFoo
created () {
this.stateFoo // -> store.state.foo
this.getterFoo // -> store.getters.foo
this.actionFoo({ value: true }) // -> store.dispatch('foo', { value: true })
this.mutationFoo({ value: true }) // -> store.commit('foo', { value: true })
}
}
TS
$ npm install -D vuex-module-decorators
$ yarn add -D vuex-module-decorators
import { VuexModule, Module, Mutation, Action } from 'vuex-module-decorators';
export interface CounterState {
counter: number;
}
@Module
export default class Counter extends VuexModule implements CounterState {
public counter = 0;
@Action({ commit: 'increment' })
public add1(): number {
return 1;
}
@Mutation
private increment(delta: number): void {
this.counter += delta;
}
get getCounter(): number {
return this.counter;
}
}
counter.ts
import Vue from 'vue';
import Vuex, { StoreOptions } from 'vuex';
import Counter from './counter';
Vue.use(Vuex);
export interface RootState {
counter: Counter;
}
const store: StoreOptions<RootState> = {
modules: {
counter,
},
};
export default new Vuex.Store<RootState>(store);
store.ts
module.exports = {
publicPath: './',
css: {
loaderOptions: {
sass: {
data: `@import '@/styles/variables.scss';`,
},
},
},
};
vue.config.js
$ npm install -S vue-cli-plugin-electron-builder
$ yarn add vue-cli-plugin-electron-builder
We are hiring !
bruce@cantarim.com
linkedin.com/in/brucecantarim
By Bruce Cantarim