Vue CLI 内置了 TypeScript 工具支持。在 Vue 的下一个大版本 (3.x) 中也计划了相当多的 TypeScript 支持改进,包括内置的基于 class 的组件 API 和 TSX 的支持。

  • 创建工程
    npm install --global @vue/cli
    vue create my-project-name:选择 "Manually select features (手动选择特性)" 选项
  • 注意5点
    1. methods 可以直接声明为类成员方法。

      <script lang="ts">
      import { Component, Prop, Vue } from 'vue-property-decorator'; @Component({
      props:{
      msg: String
      }
      }) export default class HelloWorld extends Vue {
      // @Prop() private msg!: string; // method
      greet () {
      alert('greeting: ' + this.msg)
      }
      }
      </script>
    2. 可以将计算属性声明为类属性访问器。

      <script lang="ts">
      import { Component, Prop, Vue } from 'vue-property-decorator'; @Component
      export default class HelloWorld extends Vue {
      // @Prop() private msg!: string; // computed
      get computedMsg () {
      return 'computed ' + this.msg
      }
      } </script>
    3. Initial data可以声明为类属性(如果使用Babel,则需要babel-plugin-transform-class-properties)。

      <script lang="ts">
      import { Component, Prop, Vue } from 'vue-property-decorator'; @Component
      export default class HelloWorld extends Vue {
      // @Prop() private msg!: string; // initial data
      msg =
      } </script>
    4. datarender并且所有Vue生命周期钩子也可以直接声明为类成员方法,但是您不能在实例本身上调用它们。声明自定义方法时,应避免使用这些保留名称。

    5. 对于所有其他选项,将它们传递给装饰器函数。

Vue+TypeScript学习的更多相关文章

  1. vue.js 学习笔记3——TypeScript

    目录 vue.js 学习笔记3--TypeScript 工具 基础类型 数组 元组 枚举 字面量 接口 类类型 类类型要素 函数 函数参数 this对象和类型 重载 迭代器 Symbol.iterat ...

  2. Typescript 学习笔记一:介绍、安装、编译

    前言 整理了一下 Typescript 的学习笔记,方便后期遗忘某个知识点的时候,快速回忆. 为了避免凌乱,用 gitbook 结合 marketdown 整理的. github地址是:ts-gitb ...

  3. Vue + TypeScript + Element 搭建简洁时尚的博客网站及踩坑记

    前言 本文讲解如何在 Vue 项目中使用 TypeScript 来搭建并开发项目,并在此过程中踩过的坑 . TypeScript 具有类型系统,且是 JavaScript 的超集,TypeScript ...

  4. 使用Vue-cli3搭建Vue+TypeScript项目

    一,创建项目 使用 npm 安装 vue-cli 3 和typescript npm i -g @vue/cli typescript 使用vue create命令快速搭建新项目的脚手架 vue cr ...

  5. TypeScript学习文档-基础篇(完结)

    目录 TypeScript学习第一章:TypeScript初识 1.1 TypeScript学习初见 1.2 TypeScript介绍 1.3 JS .TS 和 ES之间的关系 1.4 TS的竞争者有 ...

  6. vue入门学习(基础篇)

    vue入门学习总结: vue的一个组件包括三部分:template.style.script. vue的数据在data中定义使用. 数据渲染指令:v-text.v-html.{{}}. 隐藏未编译的标 ...

  7. Vue.js学习笔记(2)vue-router

    vue中vue-router的使用:

  8. TypeScript学习指南--目录索引

    关于TypeScript: TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程. TypeS ...

  9. TypeScript 学习一 参数,函数,析构表达式

    1,TypeScript是由谷歌开发的,并且新出的Angular2框架就是谷歌公司由TypeScript语言编写的,所以现在TypeScript是有微软和谷歌一起支持的: 2,TypeScript在j ...

随机推荐

  1. android YUV Sensor配置Camera应用的flash auto菜单

    请在Config.ftbl.flashlight.h (mediatek\custom\common\hal\flashlight\src)中. 将全部的两处凝视掉的code: //CameraPar ...

  2. Firefox中使用pac

    https://campus.barracuda.com/product/websecurityservice/article/WSS/ConfigProxyWithPACFile/ https:// ...

  3. 解决Android Studio 2.2.3中添加.cpp .h文件在Project->Android无法显示,无法正常编译问题。

    搭配使用 Android Studio 2.2 或更高版本与 Android Plugin for Gradle 版本 2.2.0 或更高版本时,您可以将 C 和 C++ 代码编译到 Gradle 与 ...

  4. Coderfroces 862 C. Mahmoud and Ehab and the xor

    C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...

  5. WebAssembly学习(五):AssemblyScript - Hello World

    一.项目创建 1.创建AssemblyScript根目录 mkdir AssemblyScript 2.安装AssemblyScript开发依赖 cnpm install --save-dev Ass ...

  6. 【J-meter】参数及相应数据中文显示乱码问题

    参考资料: http://www.51testing.com/html/00/130600-1360743.html http://www.cnblogs.com/fengpingfan/p/5851 ...

  7. 树莓派 使用python来操作GPIO 控制LED灯

    一.创建python驱动和控制GPIO 先新建一个文件夹用于放置脚本 mkdir python_gpio 进入文件夹内新建一个gpio_blink.py的脚本 cd python_gpio touch ...

  8. CSUOJ 1637 Yet Satisfiability Again!

    1637: Yet Satisfiability Again! Time Limit: 5 Sec  Memory Limit: 128 MB Description Alice recently s ...

  9. 安卓https

    http://www.tuicool.com/articles/NrmE3e http://blog.csdn.net/guestcode/article/details/50194357 http: ...

  10. 在XEN上启动guest时loopback设备不足

    现象: 通常出现在用户挂载了多个img到guest上,会发现无法再创建guest或者无法启动guest的现象. /var/log/xen/xen-hot-plug.log中错误如下: xenstore ...