Vue2 中安装 TS 使用 JSX
安装脚手架
bash
npm i @vue/cli -g
创建项目
bash
vue create 项目名称(必须小写)
正式安装步骤
bash
Vue CLI v3.3.0
? Please pick a preset: (Use arrow keys)
> default (babel, eslint)
Manually select features
它的意思是选择默认还是手动选择
空格表示选择 回车表示下一步
bash
Vue CLI v3.3.0
? Please pick a preset: Manually select features
? Check the features needed for your project: (Press <space> to select, <a> to toggle all, <i> to invert selection)
>(*) Babel
(*) TypeScript
( ) Progressive Web App (PWA) Support
(*) Router
(*) Vuex
(*) CSS Pre-processors
(*) Linter / Formatter
( ) Unit Testing
( ) E2E Testing
让你选择 vue2 还是 vue3
bash
Choose a version of Vue.js that you want to start the project with (Use arrow keys)
3.x
>2.x
是否使用 Class(类)风格装饰器
即通过 export default class Home extends Vue{} 创建 Vue 实例
bash
Use class-style component syntax? (Y/n) Y
使用 Babel 做转义
与 TypeScript 一起用于自动检测
bash
Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? (Y/n) Y
路由模式
是否选择 history 模式,启用 history 模式,项目 build 之后,可能会出现打开页面空白的情况哦 依据个人需求
bash
Use history mode for router? Y
选择一种 css 预处理器
bash
Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): (Use arrow keys)
Sass/SCSS (with dart-sass)
>Less
Stylus
选择一种代码格式化检测工具
bash
Pick a linter / formatter config: (Use arrow keys)
ESLint with error prevention only
ESLint + Airbnb config
> ESLint + Standard config
ESLint + Prettier
选择是否检测保存
- 或者是检测后提交
bash
? Pick additional lint features: (Press <space> to select, <a> to toggle all, <i> to invert selection, and <enter> to proceed)
>(*) Lint on save
( ) Lint and fix on commit
选择是否保存到 package.json
bash
Where do you prefer placing config for Babel, ESLint, etc.? (Use arrow keys)
> In dedicated config files
In package.json
是否保存为模板
bash
Save this as a preset for future projects? (y/N) y
选择模板名称
bash
Save preset as: (vue-ts)
安装依赖
bash
npm install
运行项目
bash
npm run serve
根目录下面 vue.config.js 配置
bash
// vue.config.js 配置说明
//官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
// 这里只列一部分,具体配置参考文档
module.exports = {
//别名设置
configureWebpack: {
resolve: {
alias: {
assets: "@/assets",
components: "@/components",
views: "@/views",
},
},
},
// 部署生产环境和开发环境下的URL。
// 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
//例如 https://www.my-app.com/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.my-app.com/my-app/,则设置 baseUrl 为 /my-app/。
baseUrl: process.env.NODE_ENV === "production" ? "./" : "/",
// outputDir: 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)
outputDir: "dist",
//用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
assetsDir: "assets",
//指定生成的 index.html 的输出路径 (打包之后,改变系统默认的index.html的文件名)
// indexPath: "myIndex.html",
//默认情况下,生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存。你可以通过将这个选项设为 false 来关闭文件名哈希。(false的时候就是让原来的文件名不改变)
filenameHashing: false,
// lintOnSave:{ type:Boolean default:true } 问你是否使用eslint
lintOnSave: true,
//如果你想要在生产构建时禁用 eslint-loader,你可以用如下配置
// lintOnSave: process.env.NODE_ENV !== 'production',
//是否使用包含运行时编译器的 Vue 构建版本。设置为 true 后你就可以在 Vue 组件中使用 template 选项了,但是这会让你的应用额外增加 10kb 左右。(默认false)
// runtimeCompiler: false,
/**
* 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
* 打包之后发现map文件过大,项目文件体积很大,设置为false就可以不输出map文件
* map文件的作用在于:项目打包后,代码都是经过压缩加密的,如果运行时报错,输出的错误信息无法准确得知是哪里的代码报错。
* 有了map就可以像未加密的代码一样,准确的输出是哪一行哪一列有错。
* */
productionSourceMap: false,
// 它支持webPack-dev-server的所有选项
devServer: {
/* 这里有个前提必须引入文件
const appData = require("./data.json");
const seller = appData.seller;
const goods = appData.goods;
const ratings = appData.ratings;
*/
before(app) {
app.get("/api/seller", function (req, res) {
res.json({
errno: 0,
data: seller,
});
});
app.get("/api/goods", function (req, res) {
res.json({
errno: 0,
data: goods,
});
});
app.get("/api/ratings", function (req, res) {
res.json({
errno: 0,
data: ratings,
});
});
},
host: "localhost", //也可以直接写IP地址这样方便真机测试
port: 8080, // 端口号
https: false, // https:{type:Boolean}
open: true, //配置自动启动浏览器
// proxy: 'http://localhost:4000' // 配置跨域处理,只有一个代理
// 配置多个代理
proxy: {
"/api": {
target: "<url>", //写地址
ws: true, // 允许跨域
changeOrigin: true, //允许跨域
pathRewrite: {
"^/api": "",
},
},
"/foo": {
target: "<other_url>",
},
},
},
};