typescript - 如何在没有复杂构建系统的情况下使用 Vue 和 Typescript?
问题描述
我已经花了 2 天的时间对此进行实验,到目前为止还没有取得任何进展。我有一个非常基本的 ASP.NET Core 网站,为 Typescript 2.9 配置。我希望我的前端非常简单,每页只有一个 vue 应用程序,没有复杂的构建系统或组件。
我想让某种配置至少工作。这是我当前的 tsconfig.json 文件:
{
"compilerOptions": {
"declaration": true,
"mapRoot": "/js/",
"module": "esnext",
"removeComments": true,
"sourceMap": true,
"target": "esnext"
},
"compileOnSave": true
}
(注意,我已经尝试过模块 es6、es5,也没有尝试过)
然后,在我的 wwwroot/js 文件夹中,我有 Site.ts,如下所示:
import { Vue } from "./types/vue";
var _mixin: any;
var _vue = new Vue({
el: '#vueheader',
mixins: [_mixin],
data: {
},
methods: {},
mounted: function () {
},
});
在 wwwroot/js/types 文件夹中,我有来自 vue 的 5 个 d.ts 文件以及 vue.js 文件。打字稿正确编译成 js 文件,但是当我访问主页时,我在 chrome 中收到错误消息“404,找不到文件/types/vue”
这里是编译好的 Site.js:
import { Vue } from "./types/vue";
var _mixin;
var _vue = new Vue({
el: '#vueheader',
mixins: [_mixin],
data: {},
methods: {},
mounted: function () {
},
});
//# sourceMappingURL=/js/Site.js.map
我的 html 有这个链接:
有人有想法么?请不要让我使用像 webpack 这样复杂的构建系统。我不想用 600 多个依赖项使我的构建过程变得臃肿。
解决方案
我正在使用 Vue3 并为我完成以下工作:
添加一个vue.d.ts
import Vue from "vue"
export = Vue;
export as namespace Vue;
修改tsconfig.json
设置
{
"compilerOptions": {
"module": "UMD",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
}
}
我的package.json
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"vue": "^3.0.2"
}
}
我的test.ts
const App = {
data() {
return {
counter: 0,
};
},
};
Vue.createApp(App).mount("#app");
生成test.js
var App = {
data: function () {
return {
counter: 0
};
}
};
Vue.createApp(App).mount("#app");
没有错误。
推荐阅读
- nlp - 如何根据句子中单词的含义找到特定的 WordNet 同义词集?
- javascript - 微信小程序,两个按钮同时展开
- view - 如何在 SAPUI5 中向页面内容添加片段
- amazon-web-services - AWS Athena、Parquet 和谓词下推
- mysql - 事务回滚后剩余的查询正在运行事件
- excel - 如何修复将数据插入单独的工作表但在每个条目之间留下两个空白行的用户表单,尽管代码插入到下一个空白行?
- php - 总和产品 kcal ,蛋白质和其他属性 Woocommerce
- javascript - Angular 6 event.stopPropagation() 在指令中不起作用(模板驱动的表单已经采用 event.data)
- android - 从自定义外部图像 URL 在 Mapbox Android 中绘制标记
- javascript - 同一个 flatMap 方法中的多个 http 请求