uniapp自动引入Vue3(ref,reactive...)的API、uniapp生命周期和封装hooks

未自动导入Vue3(ref,reactive...)的API和uniapp生命周期,需要在每个页面把API和uniapp生命周期的代码都重复写一遍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script setup>
  import { ref, reactive } from "vue"
  import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
   
  // 封装的hooks
  import { useList } from '@/hooks/useList.js'
  const { list, getList } = useList('/user/list','get')
  getList()
 
  const bool = ref(true)
 
  onLoad(() => {
     
  })
 
  onShow(() => {
     
  })
</script>

 

使用自动导入后,不需要在每个页面重复导入,简化的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script setup>
  // 封装的hooks
  const { list, getList } = useList('/user/list','get')
  getList()
 
  const bool = ref(true)
 
  onLoad(() => {
     
  })
 
  onShow(() => {
     
  })
</script>

 

安装 unplugin-auto-import 插件

1
npm install unplugin-auto-import -D

 

在项目文件 vite.config.js 导入并使用插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import path from 'path'
import fs from 'fs-extra'
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
 
 
export default defineConfig({
    plugins: [
        uni(),
        copyFile(),
                // 自动导入配置
        AutoImport({
            imports:[
                // 预设
                'vue',
                'uni-app',
                // 自定义预设
                {
                    '@/store': ['useStore'],
                    '@/store/modules/theme': ['useThemeStore'],
                    '@/hooks/useList': ['useList'],
                    '@/hooks/useTheme': ['useTheme'],
                    '@/hooks/useShare': ['useShare']
                }
            ]
        })
    ],
    build: {
        terserOptions: {
            compress: {
                // 发布时删除 console
                drop_console: true,
            },
        },
        rollupOptions: {
            output:{ // 输出重构  打包编译后的 文件目录 文件名称 【模块名称.时间戳】
                // 入口文件名
                entryFileNames: `${filePath}[name]${Timestamp}.js`,
                // 块文件名
                chunkFilename: `${filePath}[name]${Timestamp}.js`,
                // 资源文件名 css 图片等等
                assetFileNames: `${filePath}[name]${Timestamp}.[ext]`
            }
        }
    }
})

 

vite.config.js完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import path from 'path'
import fs from 'fs-extra'
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
 
function copyFile() {
    return {
        enforce: 'post',
        async writeBundle() {
            await fs.copy(
                path.join(__dirname, '/static_sub'),
                path.join(__dirname + '/unpackage/', 'dist', process.env.NODE_ENV === 'production' ? 'build' : 'dev', process.env.UNI_PLATFORM, '/')
            )
        },
    }
}
 
let filePath = ''
let Timestamp = ''
//编译环境判断,判断是否H5环境
if (process.env.UNI_PLATFORM === 'h5') {
  filePath = 'assets/'; //打包文件存放文件夹路径
  Timestamp = '.' + new Date().getTime();//时间戳
}
 
export default defineConfig({
    plugins: [
        uni(),
        copyFile(),
        AutoImport({
            imports:[
                // 预设
                'vue',
                'uni-app',
                // 自定义预设
                {
                    '@/store': ['useStore'],
                    '@/store/modules/theme': ['useThemeStore'],
                    '@/hooks/useList': ['useList'],
                    '@/hooks/useTheme': ['useTheme'],
                    '@/hooks/useShare': ['useShare']
                }
            ]
        })
    ],
    build: {
        terserOptions: {
            compress: {
                // 发布时删除 console
                drop_console: true,
            },
        },
        rollupOptions: {
            output:{ // 输出重构  打包编译后的 文件目录 文件名称 【模块名称.时间戳】
                // 入口文件名
                entryFileNames: `${filePath}[name]${Timestamp}.js`,
                // 块文件名
                chunkFilename: `${filePath}[name]${Timestamp}.js`,
                // 资源文件名 css 图片等等
                assetFileNames: `${filePath}[name]${Timestamp}.[ext]`
            }
        }
    }
})

 

posted @   编程民工  阅读(2359)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2021-09-27 uni使用new image()获取图片信息报错
点击右上角即可分享
微信分享提示