简体   繁体   English

如何将 TypeScript 与 Vue.js 和单文件组件一起使用?

[英]How to use TypeScript with Vue.js and Single File Components?

I have searched all over the web for a minimal, working example of a Vue.js + TypeScript setup.我在网上搜索了一个 Vue.js + TypeScript 设置的最小工作示例。 As per usual with the "modern JavaScript stack", most of these tutorials are either out-of-date despite being written just a couple of months ago or depending on a very specific setup.按照“现代 JavaScript 堆栈”的惯例,这些教程中的大多数要么是过时的,尽管是在几个月前编写的,要么取决于非常具体的设置。 There appears to be no generic, verifiable example to build on.似乎没有通用的、可验证的示例可以作为基础。

Here are some of the resources I considered:以下是我考虑的一些资源:

The basic template I use is the one provided by running vue-cli init webpack with all default options.我使用的基本模板是通过运行带有所有默认选项的vue-cli init webpack提供的。 As this produces a lot of code, I'm not pasting everything here.由于这会产生大量代码,因此我不会在此处粘贴所有内容。 If there is need for some specific excerpts, I will gladly update the question.如果需要一些具体的摘录,我很乐意更新问题。

The official Vue.js documentation is useless for my purpose because it doesn't consider setting up TypeScript with SFCs.官方的 Vue.js 文档对我来说毫无用处,因为它没有考虑使用 SFC 设置 TypeScript。 The latest I tried was the last on of the list.我尝试的最新的是列表中的最后一个。 I followed the setup precisely but it runs me into the following error on npm run dev :我精确地遵循了设置,但它让我在npm run dev上遇到以下错误:

[tsl] ERROR in /Users/[REDACTED]/ts-test/src/main.ts(12,3)
      TS2345: Argument of type '{ el: string; router: any; template: string; components: { App: { name: string; }; }; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.
  Object literal may only specify known properties, and 'router' does not exist in type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.

Can anyone shine some light on why this happens and how to resolve it?谁能阐明为什么会发生这种情况以及如何解决它? Better yet, I'd very much welcome a concise, minimal, step-by-step example of how to set up a working Vue.js + TypeScript configuration with the webpack template.更好的是,我非常欢迎一个简洁的、最小的、逐步的示例,说明如何使用webpack模板设置工作的 Vue.js + TypeScript 配置。

I have already successfully completed several client projects that run in production in Vue.js with vanilla JavaScript but this TypeScript tooling in combination with Vue.js just confuses the hell out of me.我已经成功地完成了几个在 Vue.js 中使用 vanilla JavaScript 在生产环境中运行的客户端项目,但是这个 TypeScript 工具与 Vue.js 的结合让我很困惑。

I have tried to use typescript with vue .我尝试将typescriptvue一起使用。 My personal opinion: it does not work well.我个人的看法:效果不好。 Since some vue internals are not suited for typescript :由于某些vue内部不适合typescript

  1. vuex with this.$store.dispatch('some_action') vuexthis.$store.dispatch('some_action')
  2. Vue.use , Vue.mixin and other similar things that mutate the global Vue instance Vue.useVue.mixin和其他类似的改变全局Vue实例的东西

But, while doing my research I have found these wonderful boilerplates: typescript-vue-tutorial by Daniel Rosenwasser and TypeScript-Vue-Starter by Microsoft.但是,在进行研究时,我发现了这些精彩的样板:Daniel Rosenwasser 的 typescript -vue-tutorial和 Microsoft 的 TypeScript-Vue-Starter

I have also tried to mimic vue-webpack-template with typescript by myself: https://github.com/sobolevn/wemake-vue-template我也尝试过自己用typescript模仿vue-webpack-templatehttps ://github.com/sobolevn/wemake-vue-template

There are also nice tools to make your typescript + vue workflow better:还有一些很好的工具可以让你的typescript + vue工作流程更好:

In the end I have decided to use flow .最后我决定使用flow Check this project template if you are interested.如果您有兴趣,请查看此项目模板

update更新

In Sep 2018, should try the new vue-cli with the TypeScript Plugin .在 2018 年 9 月,应该尝试使用带有TypeScript 插件的新vue-cli


Absolute agree @sobolevn's opinion.绝对同意@sobolevn 的意见。 But, there is a lot of information for me to judge, community support for TypeScript is worth the wait.但是,有很多信息让我判断,社区对 TypeScript 的支持值得等待。

Vue's ecosystem is being more TypeScript: Vue 的生态系统更像是 TypeScript:

  1. *.vue file's TypeScript lint in VSCode be supported. *.vue文件在 VSCode 中的TypeScript lint 得到支持。 Look this issue vetur .看看这个问题vetur

  2. Open Source Ecosystem.开源生态系统。

  3. New vue-cli in design.设计中的新vue-cli click me点我

So if you have time to wait, you can temporarily observe for some time.所以如果你有时间等待,你可以暂时观察一段时间。 Or, you can refer to these projects: TypeScript-Vue-Starter and A Webpack Template Fork With Typescript Support .或者,您可以参考这些项目: TypeScript-Vue-StarterA Webpack Template Fork With Typescript Support

This looks like the latest vue-cli template with a significant amount of stars and support for vue 2.5.这看起来像是最新的 vue-cli 模板,有大量的星星并支持 vue 2.5。

I don't think I saw it specifically mentioned in the other answers我认为我没有在其他答案中特别提到它

vue init ducksoupdev/vue-webpack-typescript my-project

https://github.com/ducksoupdev/vue-webpack-typescript https://github.com/ducksoupdev/vue-webpack-typescript

With the latest versions of VueJS it is possible to create a reliable & maintainable project with Vue, Typescript & JSX.使用最新版本的 VueJS,可以使用 Vue、Typescript 和 JSX 创建一个可靠且可维护的项目。 I have created a boilerplate for starters at Vue.TSX我为Vue.TSX的初学者创建了一个样板文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM