当前位置:   article > 正文

Taro+vue3+nutui3.x构建微信小程序_taro vue3 开发小程序流程

taro vue3 开发小程序流程

本文讲解了使用taro+nutui3构建微信小程序,并对组件实现按需引入的流程

在刚开始尝试的时候,一直下载的都是taro的最新版本,导致没法对组件进行按需引入,或者一些奇奇怪怪的问题。

1. 安装taro

通过cmd进入终端 

npm install -g @tarojs/cli@3.5.6    //下载3.5.6版本  可以通过taro--version验证

2.搭建项目

接下来 在磁盘创建一个文件夹 命名为myTaroApp 

点击进入此文件夹 执行cmd 进入终端

然后执行taro init myApp

这里选择vue3+ts+nutui3+sass预处理语言来构建项目

这样项目就初始化完成了

在进入项目的目录后,我们可能会发现一些问题,比如tsconfig.json文件可能会有爆红提示,

这边已经准备好了解决的办法

先执行yarn add @types/sass@1.43.1 --save-dev  安装稳定版本

再在tsconfig.json文件中 的对象里面加入
 { "types": ["sass"] }  
就可以解决这个问题啦

3.拓展

如需使用 css modules 功能,找到config文件夹下的index.js文件,将mini对象里面的enable设置为true

4.路径别名的配置

仍然config文件夹下的index.js文件中配置 这里直接附上代码 方便观看x

  1. const path = require('path');
  2. const config = {
  3. projectName: 'myApp',
  4. date: '2024-2-5',
  5. designWidth: 375,
  6. deviceRatio: {
  7. 640: 2.34 / 2,
  8. 750: 1,
  9. 828: 1.81 / 2,
  10. 375: 2 / 1
  11. },
  12. sourceRoot: 'src',
  13. outputRoot: 'dist',
  14. plugins: ['@tarojs/plugin-html','taro-plugin-pinia'],
  15. defineConstants: {
  16. },
  17. copy: {
  18. patterns: [
  19. ],
  20. options: {
  21. }
  22. },
  23. framework: 'vue3',
  24. compiler: 'webpack5',
  25. cache: {
  26. enable: false // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
  27. },
  28. sass:{
  29. data: `@import "@nutui/nutui-taro/dist/styles/variables.scss";`
  30. },
  31. mini: {
  32. postcss: {
  33. pxtransform: {
  34. enable: true,
  35. config: {
  36. selectorBlackList: ['nut-']
  37. }
  38. },
  39. url: {
  40. enable: true,
  41. config: {
  42. limit: 1024 // 设定转换尺寸上限
  43. }
  44. },
  45. cssModules: {
  46. enable: true, // 默认为 false,如需使用 css modules 功能,则设为 true
  47. config: {
  48. namingPattern: 'module', // 转换模式,取值为 global/module
  49. generateScopedName: '[name]__[local]___[hash:base64:5]'
  50. }
  51. }
  52. },
  53. alias: {
  54. '@/assests': path.resolve(__dirname, '..', 'src/assests'),
  55. '@/pages': path.resolve(__dirname, '..', 'src/pages'),
  56. '@/http': path.resolve(__dirname, '..', 'src/http'),
  57. '@/store': path.resolve(__dirname, '..', 'src/store'),
  58. '@/api': path.resolve(__dirname, '..', 'src/api'),
  59. },
  60. },
  61. h5: {
  62. publicPath: '/',
  63. staticDirectory: 'static',
  64. esnextModules: ['nutui-taro'],
  65. postcss: {
  66. autoprefixer: {
  67. enable: true,
  68. config: {
  69. }
  70. },
  71. cssModules: {
  72. enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
  73. config: {
  74. namingPattern: 'module', // 转换模式,取值为 global/module
  75. generateScopedName: '[name]__[local]___[hash:base64:5]'
  76. }
  77. }
  78. }
  79. }
  80. }
  81. module.exports = function (merge) {
  82. if (process.env.NODE_ENV === 'development') {
  83. return merge({}, config, require('./dev'))
  84. }
  85. return merge({}, config, require('./prod'))
  86. }

 这里先const path = require('path') 引入路径

然后在mini对面里面加上ailas对象去配置对应的路径  下方图片是我的文件目录

最后一步 需要在tsconfig.ts文件进行配置

  1. {
  2. "compilerOptions": {
  3. "target": "es2017",
  4. "module": "commonjs",
  5. "removeComments": false,
  6. "preserveConstEnums": true,
  7. "moduleResolution": "node",
  8. "experimentalDecorators": true,
  9. "noImplicitAny": false,
  10. "allowSyntheticDefaultImports": true,
  11. "outDir": "lib",
  12. "noUnusedLocals": true,
  13. "noUnusedParameters": true,
  14. "strictNullChecks": true,
  15. "sourceMap": true,
  16. "baseUrl": ".",
  17. "rootDir": ".",
  18. "jsx": "preserve",
  19. "allowJs": true,
  20. "resolveJsonModule": true,
  21. "typeRoots": ["node_modules/@types"],
  22. "paths": {
  23. "@/*": ["./src/*"],
  24. "@/http/*": ["./src/http/*"],
  25. "@/pages/*": ["./src/pages/*"],
  26. "@/assests/*": ["./src/assests/*"],
  27. "@/store/*": ["./src/store/*"],
  28. "@/api/*": ["./src/api/*"],
  29. }
  30. },
  31. "include": ["./src", "./types"],
  32. "compileOnSave": false,
  33. "types": ["sass"],
  34. }

配置完成后即可使用了,欢迎补充

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/777943
推荐阅读
相关标签
  

闽ICP备14008679号