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]277. Find the Celebrity 找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  2. CookiesHelper

    /// <summary> ///CookiesHelper 的摘要说明 /// </summary> public class CookiesHelper { public ...

  3. sql标量值函数,将汉字转化为拼音,无音标

    USE [db_Test]GO SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO create function [dbo].[fn_GetPinyin]( ...

  4. Golang之定义错误(errors)

    基本示例: package main //定义错误 //error 也是个接口 import ( "errors" "fmt" ) var errNotFoun ...

  5. Codeforces 612B. Wet Shark and Bishops 模拟

    B. Wet Shark and Bishops time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  6. Python和JavaScript间代码转换4个工具-乾颐堂

    Python 还是 JavaScript?虽然不少朋友还在争论二者目前谁更强势.谁又拥有着更为光明的发展前景,但毫无疑问,二者的竞争在 Web 前端领域已经拥有明确的答案.立足于浏览器平台,如果放弃 ...

  7. c++11 随机代码记录

    // RadomTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #includ ...

  8. msys2 命令行添加镜像地址

    sed -i "1iServer = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686" /etc/pacman.d/mi ...

  9. Microsoft DirectX SDK 2010 版本下载

    Microsoft DirectX SDK 2010 版本下载 Version:Date Published:9.29.19626/7/2010File name:File size:DXSDK_Ju ...

  10. Linux服务器部署系列之五—Webmin篇

    对于很多习惯使用windows的用户,在刚接触Linux的时候,要使用命令行配置Linux服务器可能会感觉难以适应.今天我们来讲解一下,Linux下的图形配置工具—Webmin,通过这款工具,用户可以 ...