使用vscode开发uniapp, 摆脱hbuilder

使用vscode开发uniapp, 摆脱hbuilder

创建项目

  • 创建uniapp项目
vue create -p dcloudio/uni-preset-vue helloproject
  • 选择模版
    这里我选择默认模版
> 默认模板
  默认模板(TypeScript)
  Hello uni-app
  前后一体登录模板
  看图模板
  新闻/资讯类模板
  自定义模板
  • 启动项目
npm i
npm run serve

我这里启动项目报错,如下:

Syntax Error: Error: PostCSS plugin autoprefixer requires PostCSS 8.
Migration guide for end-users:
https://github.com/postcss/postcss/wiki/PostCSS-8-for-end-users

安装了依赖后,正常启动了

npm i postcss-loader autoprefixer@8.0.0
  • 安装vscode插件
  1. uni-create-view: 在 VSCode 右键点击文件夹可以快速创建页面与组件,创建页面时将自动添加 pages.json
  2. uni-helper:uni-app 代码提示
  3. uniapp小程序扩展:提供创建页面和组件功能,鼠标悬停到组件上的 API 提示功能

添加 ts 类型校验

  • 安装类型声明文件
npm i -D @types/wechat-miniprogram @uni-helper/uni-app-types
  • 配置 tsconfig.json
{
  // 其他配置项
  ...,
  
  "compilerOptions": {
    "sourceMap": true,
    "paths": {
      "@/*": ["./src/*"]
    },
    "types": [
      "@dcloudio/types",
      "@types/wechat-miniprogram",
      "@uni-helper/uni-app-types"
    ]
  },
  "vueCompilerOptions": {
    "experimentalRuntimeMode": "runtime-uni-app",
    "nativeTags": [
      "block",
      "component",
      "template",
      "slot"
    ]
  },
  
  // 其他配置项
  ...
}
  • 重启VsCode

其他配置

  • 使用scss,建议使用以下版本
npm i -D sass-loader@7.3.1
npm i -D node-sass@4.14.1
  • json 注释问题
    • 打开vscode的设置,搜索文件关联
    • Files: Associations, 添加项,
    • 项: manifest.json, 值: jsonc
    • 项: pages.json, 值: jsonc

使用uni-ui组件库

  • 安装
npm i @dcloudio/uni-ui
  • 配置easycom
    打开项目根目录下的 pages.json 并添加 easycom 节点:
// pages.json
{
	"easycom": {
		"autoscan": true,
		"custom": {
			// uni-ui 规则如下配置
			"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
		}
	},

	// 其他内容
	pages:[
		// ...
	]
}
  • 配置uni-ui组件类型声明
npm i -D @uni-helper/uni-ui-types
  • 配置 tsconfig.json
    types中添加 @uni-helper/uni-ui-types@dcloudio/types
{
  // 其他配置项
  ...,
  
  "compilerOptions": {
    "sourceMap": true,
    "paths": {
      "@/*": ["./src/*"]
    },
    "types": [
      "@dcloudio/types",
      "@types/wechat-miniprogram",
      "@uni-helper/uni-app-types",
      "@uni-helper/uni-ui-types"
    ]
  },
  "vueCompilerOptions": {
    "experimentalRuntimeMode": "runtime-uni-app",
    "nativeTags": [
      "block",
      "component",
      "template",
      "slot"
    ]
  },
  
  // 其他配置项
  ...
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsd_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值