Vue对TS的支持一致不太好,连Vue作者尤大也自嘲真香压错了宝。期待Vue3.0会用TS重构且会有较大改进。不过目前有一些第三方的库可以曲线优化对TS的支持。主要就介绍下过下面两个库来写Vue。

总体体验尚可,类型检查,智能提示该有的都有,顺滑中带着一丝蹩脚。
如果要支持组件Props的类型检查及智能提示,则必须放弃template通过render写TSX, 总有种写React的感觉。

介绍

kaorun343/vue-property-decorator​

vue-property-decorator 定义了很多的装饰器,如 @prop,@component,@watch 等。已经相当齐全了,不多介绍了,而且此库已经集成进了 vue-cli 3.0中,通过cli创建的项目也集成demo页面。

wonderful-panda/vue-tsx-support​

vue-tsx-support 主要是用于支持再Vue的渲染函数中使用TSX,Vue本生是支持render JSX渲染的,在不涉及自定义Prop、事件时不使用该库也可以。但如果component中有自定义prop,event,TS的类型检查就会报错。大致逻辑是在原有的Vue类上有包装了一层,将属性、事件、作用域插槽以泛型方式传入接口定义,再将接口定义应用到TSX中。

CSS Module

Vue 默认是 scoped 方式引入css ,防止样式污染 ,通过vue模板使用也很方便。实际CSS 选择器使用 scoped 这种方式效率低于 CSS Module,使用TSX渲染时样式也只能通过CSS Module这样方式引用。这里再介绍个库 classNames ,通过这个库可以方便的组合样式名。

创建项目

使用vue-cli 3.0 创建一个项目 , 必选 typescript Babel ,其他根据需要选。创建完成后已经引入了Vue 及 TS 相关包了,也包括上面提到的 vue-property-decorator。包含了一个实例代码,npm install,npm run serve 已经可以跑起来了。

导入和配置

1. 安装 vue-tsx-support 包

npm install vue-tsx-support --save

  

2. 导入TS声明,有两种方式

编辑tsconfig.js

  ...
"include": [
"node_modules/vue-tsx-support/enable-check.d.ts",
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
]
// 注意:将exclude内的 "node_modules" 删掉,不然永远也无法被引用到了
...

  

或者在main.js中 import

import "vue-tsx-support/enable-check";

3. 删除根目录下的 shims-tsx.d.ts ,否则会报重复定义的错误。

4. 根目录新建 vue.config.js

module.exports = {
css: {
modules: true // 开启CSS module
},
configureWebpack: {
resolve: {
extensions: [".js", ".vue", ".json", ".ts", ".tsx"] // 加入ts 和 tsx
},
},
devServer: {
port: 8800 // webpack-dev-server port
}
};

  

创建视图组件

来创建个按钮组件:

 import { Component, Prop } from "vue-property-decorator";
import * as tsx from "vue-tsx-support"; export enum ButtonType {
default = "default",
primary = "primary"
} export enum ButtonSize {
large = "large",
small = "small"
}
export interface IButtonProps {
type?: ButtonType;
size?: ButtonSize;
num: number;
} @Component
export default class Button extends tsx.Component<IButtonProps> {
@Prop() public type!: ButtonType;
@Prop() public size!: ButtonSize;
@Prop({ default: 0 }) public num!: number; protected render() {
return (
<div>
<p>id:{this.num}</p>
{this.type && <p>type:{this.type}</p>}
{this.size && <p>size:{this.size}</p>}
</div>
);
}
}

再创建Container 用TSX引用组件Button:

import { Component, Prop } from "vue-property-decorator";
import { Component as tsc } from "vue-tsx-support";
import Button, { ButtonType, ButtonSize } from "./button"; interface IContainerProps {
name?: string;
} @Component
export default class Container extends tsc<IContainerProps> {
@Prop() public name!: string; protected render() {
return (
<div>
<p>container Name:{this.name}</p>
<p>{this.$slots.default}</p>
<p>
button:
<Button num={9} type={ButtonType.primary} size={ButtonSize.large} />
</p>
</div>
);
}
}

此时即使在Container 的 Render 方法同样会对 Props 进行类型检查 ,而VS Code也有智能提示和自动引入,这体验棒极了。

CSS Module 导入样式

注意:使用CSS module 前 需要在vue.config.js 中配置 css.modules = true

注意:如要添加全局样式可在 App.vue 中 @import 方式引用公用样式,这样就不会被CSS Module 加上后缀了。

然后加入TS定义

declare module "*.scss" {
const content: any;
export default content;
}

  

可以配合classnames库,更方便的操作CSS类

classnames​www.npmjs.com

示例:

vaynewang/SampleCode/vue-tsx-sample/​

如使用Typescript撸Vue(Vue2 + TS +TSX+CSS module)的更多相关文章

  1. 基于typescript编写vue的ts文件语法模板

    1 <template> 2 <div> 3 <input v-model="msg"> 4 <p>prop: {{ propMes ...

  2. 使用 typescript 开发 Vue

    基础配置: 1. 准备一个使用 vue-cli 生成的项目 2. 使用 npm 一建安装基础配置 npm i -S @types/node typescript vue-class-component ...

  3. 教你搭建基于typescript的vue项目

    自尤大去年9月推出vue对typescript的支持后,一直想开箱尝试,对于前端sr来说,vue的顺滑加上ts的面向对象,想着就非常美好~ 终于在两个月前,找到了个机会尝试了一把vue+ts的组合. ...

  4. vue cli4构建基于typescript的vue组件并发布到npm

    基于vue cli创建一个vue项目 首先安装最新的vue cli脚手架, npm install --global @vue/cli npm WARN optional SKIPPING OPTIO ...

  5. 用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象

    用TypeScript开发Vue--如何通过vue实例化对象访问实际ViewModel对象 背景 我个人很喜欢TypeScript也很喜欢Vue,但在两者共同使用的时候遇到一个问题. Vue的实例化对 ...

  6. TypeScript开发Vue

    用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象 目录 背景 解决方案 关于Vue中的计算属性类型 TypeScript的强制类型声明语法 强制类型声明的局限 ...

  7. TypeScript编写Vue项目结构解析

    使用TypeScript编写Vue项目也已经有了一段时间,笔者在刚刚使用TypeScript时候也是很茫然,不知道从何下手,感觉使用TypeScript写项目感觉很累赘并不像JavaScript那么灵 ...

  8. Typescript & React & Vue

    Typescript & React & Vue Typescript & React https://facebook.github.io/create-react-app/ ...

  9. 基于Typescript的Vue项目配置国际化

    基于Typescript的Vue项目配置国际化 简介 使用vue-i18n插件对基于Typescript的vue项目配置国际化,切换多种语言, 配合element-ui或者其他UI库 本文以配置中英文 ...

随机推荐

  1. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  2. ScrollView listView gridView 之间的冲突问题

    在ScrollView中的listView gridView添加适配器之后添加//设置gridView整体的高度 public void setListViewHeightBasedOnChildre ...

  3. 图片素材类Web原型制作分享-Pexels

    Pexels是一个高清图片下载服务站点,为用户提供海量共享图片素材的网站,每周都会定量更新. 菜单栏和底部栏都是悬浮在固定位置,内容区域滚动.首页图片排列采用瀑布流的方式,多图片滚动.包含的页面有:浏 ...

  4. Spring AOP配置

    相关概念有点拗口,我这里简单总结一个,切面,决定做什么,写处理逻辑,比如打日志.切入点,决定在哪些方里拦截,一般填正则表达式查询. 通知,就是连接切面和切入点的桥梁. 其中遇到了配置好,启动服务器没报 ...

  5. hdu-1128(数学问题,筛数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1128 思路:从0,开始,每次求一个数x的d(x),然后判断如果x没有标记,则说明x没有由任意一个d(i ...

  6. [转]一个CMake编译问题的解决过程

    问题的提出 公司的一个power-pc平台的产品,有个协议进行了修改,过程中出现了比较奇怪的情况.直接将修改后的动态库下载到设备上(原始设备是有文件系统和其他的依赖文件的,相当于部分更新应用),设备和 ...

  7. SQL 查找重复记录

    CREATE TABLE product( ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL, Pid INT NOT NULL, Pname VARCHAR(50) ...

  8. python读取文件另存为

    fr = open(filename_r,encoding='cp852') w2 = open(filename_w,'a')#a代表追加 w代表重写 for line in fr: w2.writ ...

  9. svn 创建tag

    1. 右键项目(源) 2. 选择复制到哪个路径(创建文件夹选项) 3. 选择哪个版本(源的) 4. 填写备注 5. 结束 使用:import  .合并等

  10. Creating a Simple Web Service and Client with JAX-WS

    Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...