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

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

我们再看一下项目的效果



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

接下来我们来分析代码

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

  1. //main.ts
  2. import Vue from 'vue';
  3. // vuetify: 为移动而生的Vue JS 2.0组件框架
  4. import Vuetify from 'vuetify';
  5. // Vuex store
  6. import store from './store';
  7. // Stylesheets
  8. import 'vuetify/dist/vuetify.min.css';
  9. // Components
  10. import { Sketch } from 'vue-color';
  11. import * as components from './components';
  12. // Views
  13. import Home from './views/Home.vue';
  14. Vue.use(Vuetify);
  15. Object.keys(components).forEach(name => {
  16. Vue.component(name, components[name]);
  17. });
  18. Vue.component('sketch-picker', Sketch);
  19. new Vue({
  20. el: '#app',
  21. store,
  22. render: h => h(Home)
  23. });

接下来看Home.vue页面

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

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

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

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

看到引入了

import { Getter } from '@/decorators';

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



让全局可以使用getters函数

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

  1. //Home.vue
  2. <template lang="pug">
  3. v-app#home
  4. v-toolbar.purple(dark)
  5. v-toolbar-title #[blank-link(url='https://github.com/vuejs/vuex/') Vuex] with #[blank-link(url='https://www.typescriptlang.org/') TypeScript] example
  6. main
  7. v-content
  8. v-container(fluid, grid-list-xl)
  9. v-layout(row, wrap, align-content-center, justify-space-around)
  10. v-flex(xs12, md3, order-md2)
  11. sketch-picker(:value='colour', @input='updateColor').picker-center
  12. v-flex(xs12, md4, order-md1)
  13. color-box(title='Box 1')
  14. v-flex(xs12, md4, order-md3)
  15. color-box(title='Box 2')
  16. </template>
  17. <script lang="ts">
  18. import Vue from 'vue';
  19. import Component from 'vue-class-component';
  20. import { Getter } from '@/decorators';
  21. @Component({
  22. name: 'Home'
  23. })
  24. export default class Home extends Vue {
  25. @Getter('colour') colour: string;
  26. // Actions 支持同样的载荷方式和对象方式进行分发:
  27. updateColor(newColor) {
  28. let newColorHex: string = newColor.hex;
  29. // newColorHex是deploy是载荷,传递的数据
  30. this.$store.dispatch('updateColor', newColorHex);
  31. }
  32. }
  33. </script>
  34. <style scoped>
  35. #home {
  36. margin-bottom: 1rem;
  37. }
  38. .picker-center {
  39. margin: 0 auto;
  40. }
  41. </style>

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

  1. //src\components\ColorBox.vue
  2. <template lang="pug">
  3. v-card(raised, hover)
  4. v-card-title(primary-title, v-if='hasTitle')
  5. span.title {{ title }}
  6. v-card-text
  7. .color-box(:data-color='colour', :style='boxStyle')
  8. </template>
  9. <script lang="ts">
  10. import Vue from 'vue';
  11. import Component from 'vue-class-component';
  12. import { Getter } from '@/decorators';
  13. @Component({
  14. name: 'ColorBox',
  15. props: {
  16. title: String
  17. }
  18. })
  19. export default class ColorBox extends Vue {
  20. @Getter('colour') colour: string;
  21. title: string;
  22. get hasTitle() {
  23. return !!this.title;
  24. }
  25. get boxStyle() {
  26. return {
  27. 'background-color': this.colour
  28. };
  29. }
  30. }
  31. </script>
  32. <style scoped>
  33. .color-box {
  34. width: 150px;
  35. height: 150px;
  36. margin: 0 auto;
  37. }
  38. </style>
  1. //src\components\BlankLink.vue
  2. <template lang="pug">
  3. a(:href='url', target='_blank', rel='noopener noreferrer')
  4. slot
  5. </template>
  6. <script lang="ts">
  7. import Vue from 'vue';
  8. import Component from 'vue-class-component';
  9. @Component({
  10. name: 'BlankLink',
  11. props: {
  12. url: {
  13. type: String,
  14. required: true
  15. }
  16. }
  17. })
  18. export default class BlankLink extends Vue {}
  19. </script>
  20. <style lang="scss" scoped>
  21. a {
  22. &, &:hover, &:visited {
  23. color: inherit;
  24. text-decoration: none;
  25. }
  26. &:hover {
  27. text-decoration: underline;
  28. }
  29. }
  30. </style>

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

  1. //src\store\state.ts
  2. import randomColor from 'randomcolor';
  3. export default class State {
  4. public color: string;
  5. constructor() {
  6. this.color = randomColor();
  7. }
  8. }

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

  1. //src\store\getters.ts
  2. import { GetterTree } from 'vuex';
  3. import State from './state';
  4. // GetterTree<[current state], [root state]>
  5. const getters: GetterTree<State, State> = {
  6. /*
  7. * It's just color with new name.
  8. * Example for using getters.
  9. */
  10. colour(state: State): string {
  11. return state.color;
  12. }
  13. };
  14. export default getters;

mutations同步处理函数

  1. //src\store\mutation-types.ts
  2. export const COLOR = 'COLOR';
  1. //src\store\mutations.ts
  2. import { MutationTree } from 'vuex';
  3. import { COLOR } from './mutation-types';
  4. import State from './state';
  5. const mutations: MutationTree<State> = {
  6. [COLOR](state: State, newColor: string): void {
  7. state.color = newColor;
  8. }
  9. };
  10. export default mutations;

看不懂这个

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

state.color = newColor;

}

action是进行异步处理的

  1. //src\store\actions.ts
  2. import { ActionContext, ActionTree } from 'vuex';
  3. import { COLOR } from './mutation-types';
  4. import State from './state';
  5. // ActionTree<[current state], [root state]>
  6. const actions: ActionTree<State, State> = {
  7. updateColor({ commit }: ActionContext<State, State>, newColor: string): void {
  8. commit(COLOR, newColor);
  9. }
  10. };
  11. 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. 用pymysql实现的注册登录公告练习

    import pymysql #1.连接服务器 conn=pymysql.connect( host='127.0.0.1', port=3306, user='root', password='12 ...

  2. iOS开发UIMotionEffect运动视觉效果

    1.UIMotionEffect简介 在iOS7.0推出了UIMotionEffect运动视觉效果,就是从屏幕偏移不同角度.看到的效果不同! NS_CLASS_AVAILABLE_IOS(7_0) @ ...

  3. oracle增加用户密码,cmd导入数据库

    1.tomcat中sql语句 用户名 pdmis 密码pdmis create USER pdmis IDENTIFIED BY pdmis;grant create session to pdmis ...

  4. 5.1_Spring Boot2.x安装Docker

    1.简介 Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker 是一个开源的应用容器引擎,基于Go 语言并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的应用 ...

  5. Django的日常-模型层(1)

    目录 Django的日常-模型层(1) 模型层 django测试环境 ORM查询 Django的日常-模型层(1) 模型层 模型层其实就是我们应用名下的models.py文件,我们在里面写入想要创建的 ...

  6. 继承关系中子类使用@Data注解问题

    HashSet中使用@Data注解问题 平时习惯使用lombok工具,免去了我们写get.set方法之类的,当然了,我们使用@Data注解后,equals().hashCode().toString( ...

  7. seienium基础(测试脚本中的等待方法)

    测试脚本中的等待方法 一.加等待时间的目的 等待是为了使脚本执行更加稳定 二.常用的休眠方式 第一种  sleep(): 设置固定休眠时间.python 的 time 包提供了休眠方法 sleep() ...

  8. amazeUI表单提交验证--input框required

    效果: html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  9. CF961F k-substring

    题意:给你一个字符串(sl<=1e6),问每一个起点到1和终点到sl距离相等的子串的最长不等于串长的border. 标程: #include<cstdio> #include< ...

  10. UVALive-3722 留个坑,为什么费马小定理求逆元不对??

    #include <iostream> #include <cstdlib> #include <queue> #include <algorithm> ...