摘要:脚手架安装好的基本项目项目名,如,按照提示安装你想要的东西,本次项目预装模式下,默认的项目目录如下保存自动格式化修复本人习惯缩进为个空格,但是生成的项目默认为个,因此需要更改配置文件下的更改为保存时自动修复错误保存自动格式化开启支
脚手架安装好nuxt的基本项目
npx create-nuxt-app <项目名>,如:npx create-nuxt-app nuxt-ts,按照提示安装你想要的东西,本次项目预装: Universal模式下koa+PWA+linter+prettier+axios ,默认的项目目录如下:
eslint + prettier + vscode 保存自动格式化&修复本人习惯缩进为4个空格,但是eslint&nuxt生成的项目默认为2个,因此需要更改配置
.editorconfig文件下的indent_size: 2更改为indent_size: 4
.vscode/settings.json
{
// 保存时eslint自动修复错误
"eslint.autoFixOnSave": true,
// 保存自动格式化
"editor.formatOnSave": true,
// 开启 eslint 支持
"prettier.eslintIntegration": true,
// prettier配置 --- 使用单引号【与.prettierrc下的配置对应】
"prettier.singleQuote": true,
//prettier配置 --- 结尾不加分号 【与.prettierrc下的配置对应】
"prettier.semi": false,
//prettier配置 --- 每行最多显示的字符数 【与.prettierrc下的配置对应】
"prettier.printWidth": 120,
//.vue文件template格式化支持,并使用js-beautify-html插件
"vetur.format.defaultFormatter.html": "js-beautify-html",
//js-beautify-html格式化配置,属性强制换行
"vetur.format.defaultFormatterOptions": {
"js-beautify-html": {
// "wrap_attributes": "force-aligned"
}
},
//根据文件后缀名定义vue文件类型
"files.associations": {
"*.vue": "vue"
},
//配置 ESLint 检查的文件类型
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "vue",
"autoFix": true
},
{
"language": "typescript",
"autoFix": true
}
],
"files.autoSave": "onFocusChange",
"vetur.format.enable": false,
"vetur.experimental.templateInterpolationService": true,
"editor.detectIndentation": false
}
.prettierrc文件
{
"singleQuote": true, // 使用单引号 `.vscode/settings.json` 的`prettier.semi`
"semi": false, // 结尾不加分号 `.vscode/settings.json` 的`prettier.semi`
"printWidth": 120 // 此项为我自加以上两项为默认,表示每行最多显示的字符数,默认为80,本人觉得太短了,因此修改了一下,必须与`.vscode/settings.json` 的`prettier.printWidth`对应上
/* 更多配置请戳 https://prettier.io/docs/en/options.html */
}
.eslintrc.js文件配置
module.exports = {
root: true,
env: {
browser: true,
node: true
},
parserOptions: {
parser: "babel-eslint"
},
extends: [
"@nuxtjs",
"plugin:nuxt/recommended",
"plugin:prettier/recommended",
"prettier",
"prettier/vue"
],
plugins: ["prettier"],
// add your custom rules here
rules: {
"nuxt/no-cjs-in-config": "off",
indent: ["error", 4] // 4个空格缩进
/* 更多配置请戳 http://eslint.cn/docs/rules/ */
}
}
nuxt.config.js文件下 build.extend(config, ctx) {}添加options.fix: true
build: {
/*
** You can extend webpack config here
*/
extend(config, ctx) {
// Run ESLint on save
if (ctx.isDev && ctx.isClient) {
config.module.rules.push({
enforce: "pre",
test: /.(js|vue)$/,
loader: "eslint-loader",
exclude: /(node_modules)/,
options: {
fix: true
}
})
}
}
}
开始改造工程支持typescript
安装所需插件
npm i -D @nuxt/typescript ts-node @typescript-eslint/eslint-plugin
npm install -S vue-class-component vue-property-decorator
修改&添加配置 package.json添加或编辑package.json的lint脚本:
"lint": "eslint --ext .ts,.js,.vue --ignore-path .gitignore ."
修改package.json 的 dev 脚本中 server/index.js 为 server/index.ts
"dev": "cross-env NODE_ENV=development nodemon server/index.ts --watch server",tsconfig.json
项目目录下新建tsconfig.json文件后,在package.json文件下添加:
"start-dev": "nuxt" 脚本命令,运行npm run dev就会使用默认值自动更新此配置文件
修改.eslintrc.js文件 parserOptions.parser: "@typescript-eslint/parser"
parserOptions: {
parser: "@typescript-eslint/parser"
},
修改.eslintrc.js文件 plugins添加"@typescript-eslint"
plugins: ["prettier", "@typescript-eslint"],nuxt.config.js
修改nuxt.config.js文件后缀为 nuxt.config.ts
修改nuxt.config.ts的build.extend
{
test: /.ts$/,
exclude: [/node_modules/, /vendor/, /.nuxt/],
loader: "ts-loader",
options: {
appendTsSuffixTo: [/.vue$/],
transpileOnly: true
}
}
server/index.js
修改server/index.js文件后缀为 server/index.ts
修改server/index.ts中的
const config = require("../nuxt.config.js")
// 为
const config = require("../nuxt.config.ts")
修改vue文件为typescript语法
typescript 语法如下:
坑点
vetur 报错 Cannot find module "xxxx"
解决方案:import 路径 需要写清楚后缀
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/109662.html
摘要:公司的招聘要求都提到了至少熟悉其中一种前端框架,有前端工程化与模块化开发实践经验相关字眼。我们主要从端公众号移动端小程序三大平台进行前端的技术选型,并来说说选其技术的几大优势。技术的优势互联网前端大潮后,前端出现了大框架,分别是与。 1、技术选型的背景前端技术发展日新月异,互联网上出现的新型框架也比较多,如何让新招聘的人员...
摘要:前言本文讲解如何在项目中使用来搭建并开发项目,并在此过程中踩过的坑。具有类型系统,且是的超集,在年势头迅猛,可谓遍地开花。年将会更加普及,能够熟练掌握,并使用开发过项目,将更加成为前端开发者的优势。 showImg(https://segmentfault.com/img/remote/1460000018720573); 前言 本文讲解如何在 Vue 项目中使用 TypeScript...
摘要:如果在文件中引入模块报错那么创建做兼容重启如果你在引入成功的时候,但是还是报错找不到模块等,那么请在方面找一下,相信可以解决你的问题。 背景: 因为公司要做服务器渲染配置的TS有点很坑的,故此记录一下 项目初始化 可以根据官网的安装步骤选择自己用的依赖进行安装,[详情请看][1],主要详细的说下nuxt如何配置typescript 在工程目录中创建tsconfig.json...
摘要:即将发布经过漫长的等待,即将发布。是一款很不错的组件库,虽然在的下载量仍远高于,但不可否认的是在生态和社区活跃度上,更胜一筹。 .markdown-body{word-break:break-word;line-height:1.75;font-weight:400;font-size:15px;overflow-x:hidden;color:#333}.markdown-body h1,...
摘要:注本文是我在开发过程中遇到问题及解决方法的总结,之后会持续更新,希望帮助到更多的学习者。文中有不妥的地方希望指出共同学习,同时欢迎一起补充。 注:本文是我在开发过程中遇到问题及解决方法的总结,之后会持续更新,希望帮助到更多的学习者。文中有不妥的地方希望指出共同学习,同时欢迎一起补充。 npm篇 npm安装依赖报错:permission denied,错误信息大致如下: npm ERR!...
阅读 1055·2023-04-25 20:18
阅读 2452·2021-11-22 13:54
阅读 2964·2021-09-26 09:55
阅读 4401·2021-09-22 15:28
阅读 3275·2021-09-03 10:34
阅读 2043·2021-07-28 00:15
阅读 1835·2019-08-30 14:25
阅读 1594·2019-08-29 17:16