UniApp开发
1.下载开发者工具
1.1打开官网https://www.dcloud.io/hbuilderx.html下载对应版本
1.2.新建项目,选择默认模版,自定义目录
2.将项目运行在微信小程序中
2.1打开微信小程序开发的设置界面,开启服务端口,回到,uni编辑器,再次运行,可能需要填写微信小程序开发者工具的安装路径
3.认识目录
配置文件
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
| { "pages": [ { "path": "pages/index/index", "style": { "navigationBarTitleText": "首页" } } ], "globalStyle": { "navigationBarTextStyle": "black", "navigationBarTitleText": "uni-app", "navigationBarBackgroundColor": "#F8F8F8", "backgroundColor": "#F8F8F8" }, "uniIdRouter": {}, "condition" : { "current": 0, "list": [ { "name": "", "path": "", "query": "" } ] } }
|
添加tabar
为底部添加tabar,并在tarbar对页面进行切换
1 2 3 4 5 6 7 8 9 10 11 12
| "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页" }, { "pagePath": "pages/my/my", "text": "我的" } ] }
|
在底部出现底部导航栏,添加图片,将图表放在静态文件夹下通过设置来显示
1 2 3 4
| "iconPath": "static/...",
"selectedIconPath": "static/...",
|
设置路径
设置选中的颜色
在tabar下设置selectedcolor
改为主题色
轮播图
1 2 3 4 5 6 7 8
| <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000"> <swiper-item> <view class="swiper-item"></view> </swiper-item> <swiper-item> <view class="swiper-item"></view> </swiper-item> </swiper>
|
默认全屏宽度750rpx,
点击事件@tap=“事件”
引入 uni-ui 组件库
操作步骤
安装 uni-ui 组件库
配置自动导入组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| { "easycom": { "autoscan": true, "custom": { "^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue" } }, "pages": [ ] }
|
安装类型声明文件(ts用)
1
| npm i -D @uni-helper/uni-ui-types
|
配置类型声明文件(ts用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| { "compilerOptions": { "types": [ "@dcloudio/types", "miniprogram-api-typings", "@uni-helper/uni-app-types", "@uni-helper/uni-ui-types" ] }, "vueCompilerOptions": { "nativeTags": ["block", "component", "template", "slot"] } }
|
持久化存储插件
安装持久化存储插件: pinia-plugin-persistedstate
1
| pnpm i pinia-plugin-persistedstate
|
插件默认使用 localStorage
实现持久化,小程序端不兼容,需要替换持久化 API。
基本用法
::: code-group
{28-31} [stores/modules/member.ts]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
| import { defineStore } from 'pinia' import { ref } from 'vue'
export const useMemberStore = defineStore( 'member', () => { const profile = ref<any>()
const setProfile = (val: any) => { profile.value = val }
const clearProfile = () => { profile.value = undefined }
return { profile, setProfile, clearProfile, } }, { persist:{ storage:{ getItem(key){ return uni.getStorageSync(key) }, setItem(key,value){ uni.setStorageSync(key,value) } } } }, )
|
{2,7} [stores/index.ts]1 2 3 4 5 6 7 8 9 10 11 12 13
| import { createPinia } from 'pinia' import persist from 'pinia-plugin-persistedstate'
const pinia = createPinia()
pinia.use(persist)
export default pinia
export * from './modules/member'
|
{2,8} [main.ts]1 2 3 4 5 6 7 8 9 10 11 12
| import { createSSRApp } from 'vue' import pinia from './stores'
import App from './App.vue' export function createApp() { const app = createSSRApp(App)
app.use(pinia) return { app, } }
|