搭建基于vue-cli3+typescript的tsx开发模板

搭建vue-tsx风格的开发模板

项目创建

使用vue-cli3+创建一个基于ts的模板:

vue create vue-tsx-template

# 或者使用vue ui进入GUI模式创建
vue ui

创建的时候记得勾选typescriptcss预处理器看各自喜好选择,选择内容如下:

初始化选项

等待npm/yarn安装结束后就是一个基于tsvue模板了。

vue-tsx-support

上一步中已经创建完了基于tsvue模板,但是开发方式还是如同之前的template一样,只是将script中的js部分改成了ts来书写。接下来就将模板(template)方式改成tsx的方式,这里需要借助一个库 -- vue-tsx-support

首先安装vue-tsx-support

npm install vue-tsx-support --save
# or
yarn add vue-tsx-support

安装结束后,我们需要对我们的文件做点小改动,首先我们在主入口文件main.ts中引入:

// main.ts
import "vue-tsx-support/enable-check";

// 省略。。

然后删掉src/shims-tsx.d.ts文件,避免和vue-tsx-support/enable-check声明重复冲突。

最后在我们的vue.config.js文件里的configureWebpack属性下增加一项resolve

// vue.config.js

module.exports = {
  // ...
  configureWebpack: {
    resolve: {
      extensions: [".js", ".vue", ".json", ".ts", ".tsx"] // 加入ts 和 tsx
    }
  }
}

这样就可以了,接下来就可以开始开发了。

我们在/components下新建一个button文件夹,并创建一个文件button.tsx。然后开始书写我们tsx风格的vue代码:

// components/button/button.tsx
import { Component, Prop } from "vue-property-decorator";
import * as tsc from "vue-tsx-support";

interface ButtonClick {
  (value: string): void
}

interface ButtonProps {
  text: string;
  btnClick?: ButtonClick
}

@Component
export default class ZButton extends tsc.Component<ButtonProps> {
  @Prop() text!: string;

  public btnClick(value: string): void {
    console.log("value is: ", value);
  }

  protected render() {
    return (
      <div>
        <button onClick={() =>  this.btnClick("click")}>{this.text}</button>
      </div>
    )
  }
}

这样我们就完成了一个简单的tsx组件了

接下来我们需要去views/Home.tsx中使用这个组件,删掉原来的Home.vue,并创建一个Home.tsx

// views/Home.tsx
import { Component, Vue } from "vue-property-decorator";
import { Component as tsc } from "vue-tsx-support";
import ZButton from "@/components/button/button.tsx";

@Component
export default class HomeContainer extends tsc<Vue> {
  protected render() {
    return <Zbutton text="点我!"></Zbutton>;
  }
}

最后将App.vue改成App.tsx

// App.tsx
import { Component, Vue } from "vue-property-decorator";

@Component
export default class App extends Vue {
  protected render() {
    return (
      <div id="app">
        <router-view></router-view>
      </div>
    );
  }
}

然后运行,能看到以下效果:


运行结果

就这样完成了一个简单的tsx风格的vue项目了。

mixins

新建mixins/index.ts,在index.ts中写一个vue mixin

// mixins/index.ts
import { Vue, Component } from "vue-property-decorator";

// 这里一定要做个声明 不然在组件里使用的时候会报不存在的错误
// 要对应mixin中的属性和方法
declare module "vue/types/vue" {
  interface Vue {
    mixinText: string;
    showMixinText(): void;
  }
}

@Component
export default class MixinTest extends Vue {
  public mixinText: string = "我是一个mixin";

  public showMixinText() {
    console.log(this.mixinText);
  }
}

然后在component/button/button.tsx中使用:

// component/button/button.tsx
import { Component, Prop } from "vue-property-decorator";
import * as tsc from "vue-tsx-support";

import MixinTest from "@/mixins";

interface ButtonClick {
  (value: string): void;
}

interface ButtonProps {
  text: string;
  btnClick?: ButtonClick;
}

// 在Component装饰器上注入mixin
@Component({
  mixins: [MixinTest]
})
export default class ZButton extends tsc.Component<ButtonProps> {
  @Prop() text!: string;

  public btnClick(value: string): void {
    console.log("value is: ", value);
  }

  // 点击事件中调用mixin的方法
  protected render() {
    return (
      <div>
        <button onClick={() => this.showMixinText()}>{this.text}</button>
      </div>
    );
  }
}

vuex

vuexts改造主要有两种方案,一种是基于vuex-class的方式,一种是基于vue-module-decorators的方式。

因为编码习惯的原因,喜欢在书写vuex的时候,一个module store的各个小模块都单独写成一个文件,而vue-module-decorators则是一个module store对应一个文件。所以在选择上,我选择了vuex-class,有需要的朋友也可以了解下vuex-module-decorators

安装vuex-class

npm install vue-class --save
#or
yarn add vuex-class

新建一个systemmodule,针对systemstore建立各自文件

  • state.ts
  • getter.ts
  • mutation.ts
  • mutation-type.ts
  • actions.ts

编写一个简单的例子,在vuex中存储user信息。

先来编写state中的内容:

// store/modules/system/state.ts

interface SystemState {
  user: Object
}

const state: SystemState = {
  user: {}
}

export default state;

mutation-type.ts

// store/modules/system/mutation-type.ts
interface SystemMutationType {
  SET_USER_INFO: String;
}

const Mutation_Type: SystemMutationType = {
  SET_USER_INFO: "SET_USER_INFO"
}

export default Mutation_Type;

mutation.ts

// store/modules/system/mutation.ts
import type from "./mutation-type";

const mutation: any = {
  [type.SET_USER_INFO as string](state: SystemState, user: Object) {
    state.user = user;
  }
}

export default mutation;

action.ts

import type from "./mutation-type";
import { Commit } from "vuex";

export const cacheUser = (context: { commit: Commit }, user: Object) => {
  context.commit(type.SET_USER_INFO as string, user);
}

然后建立一个index.ts将这些外抛出去:

// store/modules/system/index.ts
import state from "./state";
import mutations from "./mutation";
import * as actions from "./action";
import * as getters from "./getter";

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};

最后在store的入口文件处引用该module

// store/index.ts
import Vue from "vue";
import Vuex from "vuex";
import system from "./modules/system";

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    system
  }
});

接着我们去组件button.tsx中使用:

// components/button/button.tsx
import { Component, Prop } from "vue-property-decorator";
import * as tsc from "vue-tsx-support";
// 引入store命名空间 方便使用某个模块
import { namespace } from "vuex-class";

// 通过namespace(module name)的方式使用某个模块的store
const systemStore = namespace("system");

@Component
export default class ZButton extends tsc.Component<ButtonProps> {
  @Prop() text!: string;
  // store使用state和action 其他getter和mutation类型
  @systemStore.State("user") user!: Object;
  @systemStore.Action("cacheUser") cacheUser: any;

  public btnClick(value: string): void {
    console.log("value is: ", value);
    // 点击调用store的action方式存储user信息
    // 而state中的user信息会同步 可通过vue-tools查看
    this.cacheUser({ name: "张三", phone: "13333333333" });
  }

  // 点击事件中调用mixin的方法
  protected render() {
    return (
      <div>
        <button onClick={() => this.btnClick()}>{this.text}</button>
      </div>
    );
  }
}

三方组件库

目前主流的三方组件库都是支持ts的,且官方文档上都会提供ts下的demo以及配置。这里以有赞的vant作为例子。

安装:

npm install vant --save
#or
yarn add vant

ts下如果想要按需加载vant的话,就不能使用babel-plugin-import了,而是要使用ts-import-plugin

安装ts-import-plugin

npm install ts-import-plugin --save-dev
#or
yarn add ts-import-plugin -D

安装结束后,需要在vue.config.js中注入到webpack中使用:

// vue.config.js
const merge = require("webpack-merge");
const tsImportPluginFactory = require("ts-import-plugin");

// 将ts-import-plugin合并到webpack配置中
const webpackMergeConfig = config => {
  config.module
    .rule("ts")
    .use("ts-loader")
    .tap(options => {
      options = merge(options, {
        transpileOnly: true,
        getCustomTransformers: () => ({
          before: [
            tsImportPluginFactory({
              libraryName: "vant",
              libraryDirectory: "es",
              style: true
            })
          ]
        }),
        compilerOptions: {
          module: "es2015"
        }
      });
      return options;
    });
}

module.exports = {
  chainWebpack: config => {
    webpackMergeConfig(config);
    // ...省略
  }
}

然后就可以在组件文件中使用vant三方组件库了:

// components/index.ts
import Vue from "vue";
import { Button } from "vant";

Vue.use(Button);

button.tsx

// components/button/button.tsx
import { Component, Prop } from "vue-property-decorator";
import * as tsc from "vue-tsx-support";

@Component
export default class ZButton extends tsc.Component<ButtonProps> {
  @Prop() text!: string;

  public btnClick(value: string): void {
    console.log("value is: ", value);
  }

  // 点击事件中调用mixin的方法
  protected render() {
    return (
      <div>
        // 使用van-button
        <van-button type="primary">我是vant button</van-button>
      </div>
    );
  }
}

这里有个问题需要注意,在ts下打包的应用会丢失三方组件库的样式,而在开发环境是没有问题的。而导致该问题的发生是ts-loader不支持多线程打包
解决方式为在vue.config.js中配置parallel: false即可

欢迎star vue-tsx-template

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271