[英]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
.我尝试将
typescript
与vue
一起使用。 My personal opinion: it does not work well.我个人的看法:效果不好。 Since some
vue
internals are not suited for typescript
:由于某些
vue
内部不适合typescript
:
vuex
with this.$store.dispatch('some_action')
vuex
与this.$store.dispatch('some_action')
Vue.use
, Vue.mixin
and other similar things that mutate the global Vue
instance Vue.use
、 Vue.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-template
: https ://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:
*.vue
file's TypeScript
lint in VSCode be supported. *.vue
文件在 VSCode 中的TypeScript
lint 得到支持。 Look this issue vetur .看看这个问题vetur 。
Open Source Ecosystem.开源生态系统。
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-Starter和A 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.