Sol*_*den 14 typescript vue.js vitejs
我的设置:我通过create-vite-app模块安装了 Vue 和 Vite ,然后将“init vite-app”生成的所有包更新为 Vue 和 Vite 的最新 RC 版本。
现在我想为我的所有代码使用打字稿。首先,我只是玩了一下,然后在HelloWorld.vue的标签中添加了lang="ts"。这似乎有效,尽管我不知道 typescript 是如何从 vue 文件中转译的。
然后我尝试将main.js重命名为main.ts。现在什么都没有发生。
我在想我只需要安装打字稿,但后来它击中了我,为什么它在.vue组件中工作呢?如果我现在安装打字稿,我做错了什么吗?
为什么typescript在vue模块(HelloWorld)中工作,但是*.ts文件没有生成js?
小智 32
我将创建一个 vite 项目来逐步使用打字稿:
$ npm init vite-app <project-name>
$ cd <project-name>
$ npm install
Run Code Online (Sandbox Code Playgroud)
$ npm install typescript
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
在根文件夹中创建一个文件,如下所示:{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"isolatedModules": true,
"noEmit": true
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里查看什么是 tsconfig.json
shims-vue.d.ts
在src
文件夹中创建一个文件,如下所示:declare module "*.vue" {
import { defineComponent } from "vue";
const Component: ReturnType<typeof defineComponent>;
export default Component;
}
Run Code Online (Sandbox Code Playgroud)
该shims-vue.d.ts
文件可帮助您的 IDE 了解以什么结尾的文件.vue
。
现在,我们可以检查.ts
文件是否运行良好。
就我而言,我将main.js
文件重命名为文件夹main.ts
中的src
文件,
并修改index.html
, 12 行:
<script type="module" src="/src/main.js"></script>
Run Code Online (Sandbox Code Playgroud)
到
<script type="module" src="/src/main.ts"></script>
Run Code Online (Sandbox Code Playgroud)
最后,运行
npm run dev
Run Code Online (Sandbox Code Playgroud)
如果没有错误信息,您可以通过.ts
祝您好运来创建您的组件文件!
有一个名为vue-ts
. 所以运行npm init vite@latest my-vue-app -- --template vue-ts
设置了一个打字稿vite项目。
https://vitejs.dev/guide/#scaffolding-your-first-vite-project
更新:反映了 Olanrewaju 的评论。
归档时间: |
|
查看次数: |
7769 次 |
最近记录: |