使用vue3 + TS + Pinia + Vant4 + vite搭建商城H5项目框架

本文主要将如何利用搭建一个初始化的商城H5项目框架。初始化阶段使用的技术栈有:vue3.2、vue-router、 TS 、 Pinia 、 Vant4、Less、vite

1. 环境检测:

                        node -v 检测是否有安装node.js,未安装请先去官网安装node.js

2. 创建初始化项目: 

        终端输入: npm init vite

        自定义项目名称project name:demodemo

        依次选择Vue + TypeScript创建项目

       启动项目:   cd demodemo

                                npm install

                                npm run dev

3. 初始化项目文件

删除style.css文件,清空assets文件和components文件

新增request文件(配置网络请求和集中存储请求API),文件下新建index.ts和api.ts两个空文件 

新增store文件(数据共享仓库),文件下新建index.ts和order.ts和address.ts三个空文件 

新增views文件(集中存放各页面),文件下新建Home.vue和Login.vue和404.vue等空文件 

新增types文件(声明TS类型),文件下新建api.d.ts一个空文件

 处理App.vue文件

//App.vue

<script setup lang="ts">

</script>

<template>
  //路由占位符
  <router-view />
</template>

<style scoped lang="less">

</style>

 处理main.ts文件

import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)

app.mount('#app')

4. 样式初始化,引入vant组件库,浏览器适配

1)样式初始化

        npm i reset-css

//在main.ts中引入

import "reset-css"       //样式初始化

2)引入vant组件库

        npm i vant

//在main.ts中引入vant
import vant from "vant"
import "../node_modules/vant/lib/index.css"

3)浏览器适配

        npm i postcss-px-to-viewport --save -dev

新建文件postcss.config.cjs,配置设计稿的视口宽度:viewportWidth: 375,其余可选配置

//postcss.config.cjs文件
module.exports = {
    plugins: {
      'postcss-px-to-viewport': {
        viewportWidth: 375, // 设计稿的视口宽度
        unitPrecision: 5, // 单位转换后保留的精度
        propList: ["*"], // 能转化为vw的属性列表
        viewportUnit: "vw", // 希望使用的视口单位
        fontViewportUnit: "vw", // 字体使用的视口单位
        selectorBlackList: [], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。
        minPixelValue: 1, // 设置最小的转换数值,如果为1的话,只有大于1的值会被转换
        mediaQuery: false, // 媒体查询里的单位是否需要转换单位
        replace: true, //  是否直接更换属性值,而不添加备用属性
        exclude: undefined, // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
        include: undefined, // 如果设置了include,那将只有匹配到的文件才会被转换
        landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
        landscapeUnit: "vw", // 横屏时使用的单位
        landscapeWidth: 568, // 横屏时使用的视口宽度
      },
    },
  };

5. 自动导入常用的使用的第三方库的 API

npm i -D unplugin-auto-import

//在vite.config.ts中配置
import AutoImport from "unplugin-auto-import/vite"

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: [ 'vue', 'vue-router'] //自动导入相关函数
    })
  ],
})

6. 配置网络请求,全局配置请求的参数和返回值TS类型

        npm i axios

//在request文件下的index.ts配置

import axios from "axios"

const instance = axios.create({
    baseURL: "https://XXXXXX.com",
    timeout: 20000
})

//请求拦截器
instance.interceptors.request.use(
    config => {
        if(localStorage.getItem('token')){
            config.headers.Authorization = localStorage.getItem('token')
        }
        return config
    },
    err => {
        return Promise.reject(err)
    }
)

// 响应拦截器
instance.interceptors.response.use(
    res => {
        return res.data
    },
    err => {
        return Promise.reject(err)
    }
)

export default instance

//在request文件下的api.ts配置

import request from "./index"

export const helpCenterRes = () => request.get('/helpcenter')

export const bannerAPIRes = ( params: BannerAPIReq ):Promise<BannerAPIRes> => request.get('/banner', {params})

在types文件下api.d.ts,配置各个请求接口的参数类型和返回值类型,并在api.ts中应用

interface BannerAPIReq {
    title: string;
}

interface BannerListType {
    detail_url: string;
    picture: string;
    url: string;
}

interface BannerAPIRes {
    code: number;
    message: string;
    banner: BannerListType[];
}
//在Index.vue中使用封装的接口

<script setup lang="ts">
import { ref,onMounted } from "vue"
import { bannerAPIRes } from '../request/api'
onMounted( () => {
    getBannerRes()
})
interface BannerListType {
    detail_url: string;
    picture: string;
    url: string;
}
const bannerList = ref<BannerListType[]>([])

const getBannerRes = async() => {
    const res:any = await bannerAPIRes({
        title: 'newapp'
    })
    console.log(res)
    if(res.code == 200){
        bannerList.value = res.banner
    }
}
</script>

<template>
  <van-swipe :autoplay="3000" :height="420" lazy-render>
    <van-swipe-item v-for="item in bannerList">
      <img style="width: 100%;height: 419.4px;" :src="item.picture" />
    </van-swipe-item>
  </van-swipe>
</template>

<style scoped lang="less">

</style>

7.  配置路由

npm i vue-router          //下载vue-router插件第四版本

//main.ts中引入和挂载路由

import router from './router'

app.use(router) 
//router/index.ts

import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import Home from "../views/Home.vue"
import Index from "../views/Index.vue"

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component:  Home,
        redirect: '/index',   //路由重定向
        children: [           //路由嵌套
            {
                path: 'index',
                name: 'Index',
                component: Index
            },
            {
                name: 'Wash',
                path: 'wash',
                component: () => import('../views/Wash.vue')
            },
            {
                name: 'Order',
                path: 'order',
                component: () => import('../views/Order.vue')
            },
            {
                name: 'My',
                path: 'my',
                component: () => import('../views/My.vue')
            }
        ]
    },
    {
        name: 'Login',
        path: '/login',
        component: () => import('../views/Login.vue')
    },
    {
        name: "404",
        path: "/:pathMatch(.*)*",
        component: () => import('../views/404.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach( ( news,old,next ) => {
    next()
})

export default router

8. 配置pinia,并做持久化存储

npm i pinia

npm i pinia-plugin-persist

//在store/index.ts配置pinia和共享数据持久化处理

import { createPinia } from "pinia";
import piniaPluginPersist from "pinia-plugin-persist"

const store = createPinia()
store.use(piniaPluginPersist)

export default store

//在mian.ts中引入并挂载pinia

import store from "../src/store/index"

app.use(store)

为了避免共享的数据混乱放在一个文件下,因此需模块化处理不同页面的共享数据

order.ts表示订单页面涉及到的共享数据及数据的处理方法

address.ts表示地址页面涉及到的共享数据及数据的处理方法

以order.ts为示例

import { defineStore } from "pinia";

export const orderStore = defineStore( "order", {
    //存放需要共享的数据
    state: () => {
        return {
            orderType: "takein",
            count: 1
        }
    },
    //Store 状态的计算值
    getters: {
        doubleCount: (state) => state.count  * 2
    },
    //存放同步和异步方法
    actions: {
        countAdd(num: number){
            this.count = this.count + num
        }
    },
    //开启数据持久化存储
    persist: {
        enabled: true
    }
})
//在页面使用store中的数据
<script setup lang="ts">
  import { storeToRefs } from 'pinia';
  import { orderStore } from '../store/order'; //引入order模块数据
  
  const orderstore = orderStore()
  const {count, doubleCount} = storeToRefs(orderstore) //解构成响应式数据
  const addNum = (num: number) => {
    orderstore.countAdd(num)  //用actions中的方法
  }
</script>

<template>
  <div>state中的数据响应式展示: {{ count }} </div>
  <div>getters中的数据响应式展示: {{ doubleCount }} </div>  
  <button @click="addNum(10)">点击操作state中的数据</button>
</template>

<style scoped lang="less">

</style>

 开启数据持久化存储后,刷新页面,数据不会变成原始数据

9. 设置网站logo和网站名称

在index.html文件配置网站logo和网站名称

网站logo的是.ico格式,其他格式图片需要转成.ico文件

<head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/tidylogo.ico" />    //配置logo
    <title>XXXXX有限公司官方网站</title>          //配置网站名称
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>

10. 配置less

        npm i less -D

<style scoped lang="less">
@color: pink;   //定义一个颜色变量

//类名嵌套
.title{
  div{
    color: @color;   //使用变量
  }
}
</style>

11. 首页配置自定义tabbar底部栏

//Home.vue中配置tabbar栏数据和跳转路由等,同时在router/index.ts中配置相应的路由

<script setup lang="ts">
  import { ref,reactive,onMounted } from "vue"
  onMounted( () => {
    
  })

  const active = ref(0)

  const tabbarList = reactive([
    {
      title: '首页',
      path: '/index',
      inactive: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/home.png',
      active: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/home_active.png'
    },
    {
      title: '洗衣',
      path: '/wash',
      inactive: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/wash.png',
      active: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/wash_active.png'
    },
    {
      title: '订单',
      path: '/order',
      inactive: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/order.png',
      active: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/order_active.png'
    },
    {
      title: '我的',
      path: '/my',
      inactive: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/my.png',
      active: 'https://tidyimages.oss-cn-hangzhou.aliyuncs.com/newh5/my_active.png'
    }
  ])
  const onChangeTab = (index: number | string) => {
    console.log(index)
  }
</script>

<template>
  <router-view></router-view>
  <van-tabbar v-model="active" active-color="#26D7CC" route @change="onChangeTab">
    <van-tabbar-item replace :to="item.path" v-for="(item,index) in tabbarList" :key="index">
      <span :class="active === index ? active : ''">{{ item.title }}</span>
      <template #icon="props">
        <img :src="props.active ? item.active : item.inactive" />
      </template>
    </van-tabbar-item>
  </van-tabbar>
</template>

<style scoped lang="less">

</style>

效果图如下:


​​​​​​​

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你好!对于使用 Vue 3、TypeScriptVite搭建项目,并结合 Pinia 进行状态管理,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了 Node.js 和 npm(或者使用 yarn)。 2. 创建一个新的项目文件夹,并进入该文件夹。 3. 在终端中运行以下命令来初始化一个新的 Vue 3 项目: ``` npm init vite@latest ``` 在初始化过程中,选择使用 Vue 3、TypeScript 和默认配置。 4. 进入项目文件夹并安装依赖: ``` cd <project-folder> npm install ``` 5. 接下来,安装 Pinia: ``` npm install pinia ``` 6. 在 `src` 目录下创建一个 `store` 文件夹,并在其中创建名为 `index.ts` 的文件。 7. 在 `index.ts` 中编写你的 Pinia store。例如,你可以创建一个名为 `counter` 的 store,并且在其中定义一个状态和一些操作: ```typescript import { defineStore } from 'pinia'; export const useCounterStore = defineStore('counter', { state: () => ({ count: 0, }), actions: { increment() { this.count++; }, decrement() { this.count--; }, }, }); ``` 8. 在应用的入口文件 `main.ts` 中导入并使用 Pinia: ```typescript import { createApp } from 'vue'; import { createPinia } from 'pinia'; import App from './App.vue'; const app = createApp(App); const pinia = createPinia(); app.use(pinia); app.mount('#app'); ``` 9. 在组件中使用 Pinia store。在你的 Vue 组件中,可以使用 `useStore` 函数来访问 Pinia store: ```typescript import { useCounterStore } from '../store'; export default { setup() { const counterStore = useCounterStore(); return { counterStore, }; }, }; ``` 10. 最后,你可以在组件中使用 `counterStore` 来访问状态和操作: ```vue <template> <div> <p>{{ counterStore.count }}</p> <button @click="counterStore.increment()">Increment</button> <button @click="counterStore.decrement()">Decrement</button> </div> </template> <script> import { useCounterStore } from '../store'; export default { setup() { const counterStore = useCounterStore(); return { counterStore, }; }, }; </script> ``` 这样,你就可以使用 Vue 3、TypeScriptVite 和 Pinia 搭建一个基本的项目并进行状态管理了。希望对你有帮助!如果还有其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值