使用 typescript 开发 Vue
基础配置:
1.
准备一个使用 vue-cli 生成的项目
2.
使用 npm 一建安装基础配置
npm i -S @types/node typescript vue-class-component vue-property-decorator vuex vuex-class ts-loader@3.2.0
// vue-cli 的 webpack 大版本为 3
// 所以不支持 ts-loader 4以上
3.
修改配置文件
3.1 文件 bulid/webpack.base.conf.js
resolve: {
extensions: ['.js', '.vue', '.json', '.ts' ,'.tsx'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src')
}
}
entry: {
app: './src/main.ts'
}
rules: [
//... 省略 Vue js png 等 loader
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
"babel-loader",
{
loader: "ts-loader",
options: { appendTsxSuffixTo: [/\.vue$/] }
}
]
}
]
3.2 在 src 下新建文件 vue-shim.d.ts ,用于识别单文件vue内的ts代码
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
3.3 修改 main.js 后缀为 main.ts
修改 main.ts 里
import App from './App' 为 import App from './App.vue'
3.4 在根目录下添加 tsconfig.json 文件
{
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"allowJs": true,
"jsx": "preserve",
"strictFunctionTypes": false,
"module": "esnext",
"target": "es5",
"moduleResolution": "node",
"isolatedModules": false,
"lib": [
"dom",
"es5",
"es6",
"es7",
"es2015.promise"
],
"sourceMap": true,
"pretty": true
}
}
3.5 router 里的 index.js 可以选择 ts 结尾,不过影响不大
3.6 如果需要使用 router 的钩子则需要
在 src 目录下新建文件 class-components-hooks.ts
import Component from 'vue-class-component'
// Register the router hooks with their names
Component.registerHooks([
'beforeRouteEnter',
'beforeRouteLeave',
'beforeRouteUpdate' // for vue-router 2.2+
])
在 main.ts 中
import './class-components-hooks'
使用
可查看 app.vue
vue-property-decorator 的使用
import { Component, Emit, Inject, Model, Prop, Provide, Vue, Watch } from 'vue-property-decorator'
const s = Symbol('baz')
@Component
export class MyComponent extends Vue {
@Emit()
addToCount(n: number){ this.count += n }
@Emit('reset')
resetCount(){ this.count = 0 }
@Inject() foo: string
@Inject('bar') bar: string
@Inject({from: 'optional', default: 'default'}) optional: string
@Inject(s) baz: string
@Model('change') checked: boolean
@Prop()
propA: number
@Prop({ default: 'default value' })
propB: string
@Prop([String, Boolean])
propC: string | boolean
@Provide() foo = 'foo'
@Provide('bar') baz = 'bar'
@Watch('child')
onChildChanged(val: string, oldVal: string) { }
@Watch('person', { immediate: true, deep: true })
onPersonChanged(val: Person, oldVal: Person) { }
}
详情使用查看 https://github.com/kaorun343/vue-property-decorator
vue-class 使用:
@Component
export default class Hello extends Vue {
helloMsg = 'hello,grewer';
@State userName // 获取 vuex 中 state 的 userName
}
详情使用查看 https://github.com/ktsn/vuex-class
使用 element-ui 和 axios
下载:
npm i -S axios element-ui
element 的使用已经不需要额外的添加
使用 axios 的话需要添加声明
import axios from 'axios'
Vue.prototype.axios = axios
declare module "vue/types/vue" {
interface Vue {
axios:any
}
}
在使用 refs 时也需要特使的声明:
const ele:any = this.$refs.ele
ele.func()
如果还有什么不明白的可以看我的 github 里面有详细的配置
地址:https://github.com/Grewer/vue-with-typescript
使用 typescript 开发 Vue的更多相关文章
- 用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象
用TypeScript开发Vue--如何通过vue实例化对象访问实际ViewModel对象 背景 我个人很喜欢TypeScript也很喜欢Vue,但在两者共同使用的时候遇到一个问题. Vue的实例化对 ...
- TypeScript开发Vue
用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象 目录 背景 解决方案 关于Vue中的计算属性类型 TypeScript的强制类型声明语法 强制类型声明的局限 ...
- Vue使用Typescript开发编译时提示“ERROR in ./src/main.ts Module build failed: TypeError: Cannot read property 'afterCompile' of undefined”的解决方法
使用Typescript开发Vue,一切准备就绪.但npm start 时,提示“ ERROR in ./src/main.tsModule build failed: TypeError: Cann ...
- TypeScript编写Vue项目结构解析
使用TypeScript编写Vue项目也已经有了一段时间,笔者在刚刚使用TypeScript时候也是很茫然,不知道从何下手,感觉使用TypeScript写项目感觉很累赘并不像JavaScript那么灵 ...
- 001——Typescript 介绍 、Typescript 安 装、Typescript 开发工具
一. Typescript 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 4. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.Typ ...
- 使用 VS Code 搭建 TypeScript 开发环境
使用 VS Code 搭建 TypeScript 开发环境 TypeScript 是 JavaScript 的超集,TypeScript 只是增强了 JavaScript 而非改变了 JavaScri ...
- 搜狗高级架构师帮你系统掌握TypeScript开发
JavaScript 是一门动态弱类型语言,对变量的类型非常宽容,而且不会在这些变量和它们的调用者之间建立结构化的契约. Angular 已经使用 TypeScript 重构了代码,另一大前端框架 V ...
- 如何用TypeScript开发微信小程序
微信小程序来了!这个号称干掉传统app的玩意儿虽然目前处于内测阶段,不过目前在应用号的官方文档里已经放出了没有内测号也能使用的模拟器了. 工具和文档可以参考官方文档:https://mp.weixin ...
- 使用TypeScript开发
学习过一段时间CoffeeScript,然后再学习TypeScript,最后还是决定使用TypeScript开发. CofeeScript主要是给js添加一些语法糖,编写代码要快捷的多,少量的代码开发 ...
随机推荐
- MarkDown语法和使用
MarkDown语法: Markdown在线编辑器 MdEditor Markdown 语法整理大集合2017 MarkDown 数学公式 在Markdown中输入数学公式(MathJax) \(\l ...
- linux内核开发程序风格
变量命名法 这里是linux不是windows,所以匈牙利命名法是不允许使用的,在内核中,局部变量只要可以明确表达自己的意思,可以使用idx,i这种名字的id, 全局函数和变量需要有表达性的名字例如g ...
- 解决windows安装TensorFlow2.0beta版本时ERROR: Cannot uninstall 'wrapt'问题
pip install -U --ignore-installed wrapt enum34 simplejson netaddr 参考:https://bugs.launchpad.net/rall ...
- [bzoj1042][HAOI2008][硬币购物] (容斥原理+递推)
Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...
- Eclipse不编译解决方案
原文链接:http://blog.csdn.net/huahuagongzi99999/article/details/7719882 转来自己用 这两天Eclipse 不编译了,无论怎么更改保 ...
- 主流图数据库Neo4J、ArangoDB、OrientDB综合对比:架构分析
主流图数据库Neo4J.ArangoDB.OrientDB综合对比:架构分析 YOTOY 关注 0.4 2017.06.15 15:11* 字数 3733 阅读 16430评论 2喜欢 18 1: 本 ...
- 【ZJOI2017 Round2练习】
………………………………………………………………………… DAY1:听说是湖南的题 T1:spaly?毫无想法,写个暴力压压惊 T2:尼克杨问号脸 T3:FFT我不会啊,70points已经尽力了
- Ubuntu查看和写入系统日志
一.背景 Linux将大量事件记录到磁盘上,它们大部分以纯文本形式存储在/var/log目录中.大多数日志条目通过系统日志守护进程syslogd,并被写入系统日志. Ubuntu包括以图形方式或从命令 ...
- 使用Vundle管理配置Vim的插件
1.介绍: 安装需要Git,触发git clone,默认将每一个指定特定格式插件的仓库复制到~/.vim/bundle/. 搜索需要Curl支持. Windows用户请直接访问Windows setu ...
- Canopy算法计算聚类的簇数
Kmeans算是是聚类中的经典算法.步骤例如以下: 选择K个点作为初始质心 repeat 将每一个点指派到近期的质心,形成K个簇 又一次计算每一个簇的质心 until 簇不发生变化或达到最大迭代次数 ...