我不惮以最大的赞美去赞美这样的项目,真的是非常有创意又有能力。

先放上我喜欢的这个项目的开源地址:https://github.com/gluons/vuex-typescript-example

我们再看一下项目的效果



这是一个可以存储颜色的取色器呢,刷新页面会发现颜色是有保存的,取色器中的颜色改变,左右两个box的颜色也会改变。

接下来我们来分析代码

main.ts中一般定义全局公共组件和样式等

//main.ts
import Vue from 'vue';
// vuetify: 为移动而生的Vue JS 2.0组件框架
import Vuetify from 'vuetify'; // Vuex store
import store from './store'; // Stylesheets
import 'vuetify/dist/vuetify.min.css'; // Components
import { Sketch } from 'vue-color';
import * as components from './components'; // Views
import Home from './views/Home.vue'; Vue.use(Vuetify); Object.keys(components).forEach(name => {
Vue.component(name, components[name]);
});
Vue.component('sketch-picker', Sketch); new Vue({
el: '#app',
store,
render: h => h(Home)
});

接下来看Home.vue页面

Home.vue中还使用了jade的写法,标签都不需要呢~

<template lang="pug">
v-app#home
v-toolbar.purple(dark)
v-toolbar-title #[blank-link(url='https://github.com/vuejs/vuex/') Vuex] with #[blank-link(url='https://www.typescriptlang.org/') TypeScript] example
main
v-content
v-container(fluid, grid-list-xl)
v-layout(row, wrap, align-content-center, justify-space-around)
v-flex(xs12, md3, order-md2)
sketch-picker(:value='colour', @input='updateColor').picker-center
v-flex(xs12, md4, order-md1)
color-box(title='Box 1')
v-flex(xs12, md4, order-md3)
color-box(title='Box 2')
</template>

很有意思的项目啊,当开源者的水平越高超,你能够欣赏到的代码的快感就越强烈,越会有相逢恨晚的感觉

接下来我们看Home.vue中的ts部分

看到引入了

import { Getter } from '@/decorators';

我猜测那个时候,可能还没有装饰器的功能,看到这里将getters封装了一层,好像是将getters变成store的全局变量



让全局可以使用getters函数

Home页面中有一个取色器组件,他有一个updateColor方法

//Home.vue
<template lang="pug">
v-app#home
v-toolbar.purple(dark)
v-toolbar-title #[blank-link(url='https://github.com/vuejs/vuex/') Vuex] with #[blank-link(url='https://www.typescriptlang.org/') TypeScript] example
main
v-content
v-container(fluid, grid-list-xl)
v-layout(row, wrap, align-content-center, justify-space-around)
v-flex(xs12, md3, order-md2)
sketch-picker(:value='colour', @input='updateColor').picker-center
v-flex(xs12, md4, order-md1)
color-box(title='Box 1')
v-flex(xs12, md4, order-md3)
color-box(title='Box 2')
</template> <script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Getter } from '@/decorators'; @Component({
name: 'Home'
})
export default class Home extends Vue {
@Getter('colour') colour: string;
// Actions 支持同样的载荷方式和对象方式进行分发:
updateColor(newColor) {
let newColorHex: string = newColor.hex;
// newColorHex是deploy是载荷,传递的数据
this.$store.dispatch('updateColor', newColorHex);
}
}
</script> <style scoped>
#home {
margin-bottom: 1rem;
}
.picker-center {
margin: 0 auto;
}
</style>

我还是很疑问的,因为colorBox并没有在Home.vue中定义,以致于我没有结合colorBox进行分析项目

//src\components\ColorBox.vue
<template lang="pug">
v-card(raised, hover)
v-card-title(primary-title, v-if='hasTitle')
span.title {{ title }}
v-card-text
.color-box(:data-color='colour', :style='boxStyle')
</template> <script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Getter } from '@/decorators'; @Component({
name: 'ColorBox',
props: {
title: String
}
})
export default class ColorBox extends Vue {
@Getter('colour') colour: string; title: string; get hasTitle() {
return !!this.title;
}
get boxStyle() {
return {
'background-color': this.colour
};
}
}
</script> <style scoped>
.color-box {
width: 150px;
height: 150px;
margin: 0 auto;
}
</style>
//src\components\BlankLink.vue
<template lang="pug">
a(:href='url', target='_blank', rel='noopener noreferrer')
slot
</template> <script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component'; @Component({
name: 'BlankLink',
props: {
url: {
type: String,
required: true
}
}
})
export default class BlankLink extends Vue {}
</script> <style lang="scss" scoped>
a {
&, &:hover, &:visited {
color: inherit;
text-decoration: none;
}
&:hover {
text-decoration: underline;
}
}
</style>

state里面定义color,color是个变量

//src\store\state.ts
import randomColor from 'randomcolor'; export default class State {
public color: string; constructor() {
this.color = randomColor();
}
}

getters中的color,从原有的定义中取出来

//src\store\getters.ts
import { GetterTree } from 'vuex'; import State from './state'; // GetterTree<[current state], [root state]>
const getters: GetterTree<State, State> = {
/*
* It's just color with new name.
* Example for using getters.
*/
colour(state: State): string {
return state.color;
}
}; export default getters;

mutations同步处理函数

//src\store\mutation-types.ts
export const COLOR = 'COLOR';
//src\store\mutations.ts
import { MutationTree } from 'vuex'; import { COLOR } from './mutation-types';
import State from './state'; const mutations: MutationTree<State> = {
[COLOR](state: State, newColor: string): void {
state.color = newColor;
}
}; export default mutations;

看不懂这个

[COLOR](state: State, newColor: string): void {

state.color = newColor;

}

action是进行异步处理的

//src\store\actions.ts
import { ActionContext, ActionTree } from 'vuex'; import { COLOR } from './mutation-types';
import State from './state'; // ActionTree<[current state], [root state]>
const actions: ActionTree<State, State> = {
updateColor({ commit }: ActionContext<State, State>, newColor: string): void {
commit(COLOR, newColor);
}
}; export default actions;

updateColor这个更新函数,就可以和updateColor进行结合了

【心无旁骛】vuex-typescript-example的更多相关文章

  1. [Vuex] Use Namespaces in Vuex Stores using TypeScript

    Even by using modules, they still share the same namespace. So you couldn’t have the same mutation n ...

  2. [Vuex] Split Vuex Store into Modules using TypeScript

    When the Vuex store grows, it can have many mutations, actions and getters, belonging to different c ...

  3. [Vuex] Perform Async Updates using Vuex Actions with TypeScript

    Mutations perform synchronous modifications to the state, but when it comes to make an asynchronous ...

  4. [Vuex] Create a Vuex Store using TypeScript

    A Vuex store centralizes the state of your app, making it easy to reason about your state flow. In t ...

  5. Vuex 刷新后数据丢失问题 Typescript

    问题描述:Vuex保存的数据在页面刷新后会全部丢失清除 问题解决方案:使用sessionstorage进行保存,在页面刷新时保存至sessionStorage,页面在加载时再进行填充   (另有vue ...

  6. [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript

    Sometimes we need to create modules at runtime, for example depending on a condition. We could even ...

  7. Vue练手项目(包含typescript版本)

    本项目的git仓库https://github.com/lznism/xiachufang-vue 对应的使用typescript实现的版本地址https://github.com/lznism/xi ...

  8. Vue 爬坑之路(十)—— Vue2.5 + Typescript 构建项目

    Typescript 在前端圈已经逐渐普及,Vue 2.5.0 改进了类型声明,使得对 TypeScript 更加友好 不过要想在项目中直接使用 TypeScript  仍然需要对项目进行一些改造 P ...

  9. 搭建项目vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize

    vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize https://www.cnblogs.com/wuguanglin ...

  10. Vue2.5 Web App 项目搭建 (TypeScript版)

    参考了几位同行的Blogs和StackOverflow上的许多问答,在原来的ng1加TypeScript以及Webpack的经验基础上,搭建了该项目,核心文件如下,供需要的人参考. package.j ...

随机推荐

  1. 应用上云新模式,Aliware 全家桶亮相杭州云栖大会

    全面上云带来的变化,不仅是上云企业数量上的攀升,也是企业对云的使用方式的转变,越来越多的企业用户不仅将云作为一种弹性资源,更是开始在云上部署架构和应用,借助 Serverless 等技术,开发人员只需 ...

  2. Parse-轻松构建移动APP的后台服务

    目前正在开发的产品告一段落,有时间总结下经验,也顺便分享一下我们主要使用的平台-Parse. 什么是Parse?  Parse是一群美国人开发的专为移动APP服务的云计算平台,与现有的其他云计算平台相 ...

  3. hdu多校第三场 1006 (hdu6608) Fansblog Miller-Rabin素性检测

    题意: 给你一个1e9-1e14的质数P,让你找出这个质数的前一个质数Q,然后计算Q!mod P 题解: 1e14的数据范围pass掉一切素数筛法,考虑Miller-Rabin算法. 米勒拉宾算法是一 ...

  4. YARN 原理简介

    YARN 组件 参考:Spark on Yarn | Spark,从入门到精通 YARN 采用 Master/Slave结构 ,包含ResourceManager 和 NodeManager Reso ...

  5. 高性能代理缓存服务器—Squid

    Squid是什么? Squid是一款比较知名的开源代理缓存软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟.目前使用Squid的用户也是十分广泛的. Squ ...

  6. <Django> MVT三大块之Models(模型)

    1.ORM(对象-关系-映射)---面向对象,不需要面向SQL语句 根据对象的类型生成表结构 将对象.列表的操作,转化成SQL语句 将SQL语句查询的结果转化成对象.列表 目的:实现数据模型与数据库的 ...

  7. Ubuntu 18.04/18.10快速开启Google BBR的方法

    说明:Ubuntu 18.04改变挺大的,内核直接升到了正式版4.15,而BBR内核要求为4.9,也就是说满足了,所以我们不需要换内核就可以很快的开启BBR,这里简单说下方法. 提示:Ubuntu 1 ...

  8. sql 根据列名查所属表名

    比如 有一个jueseID字段,想知道这个字段是哪个表里的. 第一步: select * from syscolumns where name = 'jueseID' 第二步: select * fr ...

  9. Clover config.plist Boot部分

    <key>Boot</key> <dict> <key>Arguments</key> < nv_disable= kext-dev- ...

  10. Java虚拟机性能管理神器 - VisualVM(2) 入门【转】

    Java虚拟机性能管理神器 - VisualVM(2) 入门[转] 标签: java插件jvm监控工具入门 2015-03-11 16:54 955人阅读 评论(0) 收藏 举报  分类: Visua ...