GithubHelp home page GithubHelp logo

yulimchen / vue3-h5-template Goto Github PK

View Code? Open in Web Editor NEW
768.0 768.0 187.0 4.23 MB

🌱 A ready-to-use mobile project base template built with the Vue3, Vant, and Vite. | 基于 Vue3、Vite4、TypeScript/JavaScript、Tailwindcss、Vant4,开箱即用的移动端项目基础模板

Home Page: https://yulimchen.github.io/vue3-h5-template/

License: MIT License

JavaScript 12.77% HTML 6.22% Vue 25.06% Less 3.90% TypeScript 51.35% CSS 0.24% Shell 0.47%
app demo eruda h5 i18n javascript js mobile pinia starter tailwindcss template ts typescript vant vant4 vite vue vue3 vw

vue3-h5-template's Introduction

logo_melomini

Vue3 h5 template

🌱 基于 Vue3 全家桶、TS/JS、Vite 构建工具,开箱即用的移动端项目基础模板

  • ⚡ Vue3 + Vite4
  • 🍕 TypeScript
  • ✨ Vant4 组件库
  • 🌀 Tailwindcss 原子类框架
  • 🍍 Pinia 状态管理
  • 🌓 支持深色模式
  • 🧀 支持 i18n
  • Vue-router 4
  • 支持 SVG 图标自动注册组件
  • vw 视口适配
  • Axios 封装
  • 打包资源 gzip 压缩
  • 开发环境支持 Mock 数据
  • ESLint
  • 首屏加载动画
  • 开发环境调试面板
  • 生产环境 CDN 依赖

如有 i18n 国际化多语言需求,请切换到 i18n 分支

主分支默认 TypeScript,如果你希望使用的是 JavaScript 语言,请切换 js-version 分支进行开发

在线预览Preview

👓 点击这里(PC浏览器请切换手机端模式)

截图

运行项目

注意:要求 Node 版本 16+,可使用 nvm 进行本地 Node 版本管理,同时建议使用 pnpm 包管理器。

# 克隆项目
git clone https://github.com/yulimchen/vue3-h5-template.git

# 进入项目目录
cd vue3-h5-template

# 安装依赖
pnpm install

# 启动服务
pnpm dev

i18n 国际化多语言版本 clone 方式

# 克隆项目
git clone -b i18n https://github.com/yulimchen/vue3-h5-template.git

# 同上进入目录&安装依赖等
cd vue3-h5-template

如果你需要的是基于 JavaScript 构建的 H5 项目模板,可切换到 js-version 分支进行开发

# 克隆项目
git clone -b js-version https://github.com/yulimchen/vue3-h5-template.git

# 同上进入目录&安装依赖等
cd vue3-h5-template

文档引导

- 按需引入 vant 组件

全量引入组件库太过臃肿,项目中使用 unplugin-vue-components 插件进行按需自动引入组件,可通过官方文档了解更多。

- SVG 图标使用

  1. 将 svg 图标文件放在 src/icons/svg 目录下
  2. 在项目中直接使用 <svg-icon name="svg图标文件命名" /> 即可

例如:

本项目 src/icons/svg 中放了个叫 check-in.svg 的图标文件,然后在组件 name 属性中填入文件的命名即可,So easy~

<svg-icon name="check-in" />

项目中使用了 unplugin-vue-components 自动引入组件,所以 main.ts 中无需注册全局图标组件。

- 路由缓存 & 命名注意 ⚠

组件默认开启缓存,如某个组件需关闭缓存,在对应路由 meta 内的 noCache 字段赋值为 true 即可。

// src/router/routes.ts
const routes: Array<RouteRecordRaw> = [
    // ...
    {
        path: "about",
        name: "About",
        component: () => import("@/views/about/index.vue"),
        meta: {
            title: "关于",
            noCache: true
        }
    }
];

为了保证页面能被正确缓存,请确保组件name 值和对应路由的 name 命名完全相同。

<!-- src/views/about/index.vue -->
<script setup lang="ts" name="About">
	// 使用了 `vite-plugin-vue-setup-extend` 插件,可在 `setup` 语法糖标签上添加 `name` 属性为组件命名
</script>

<template>
  <div>about</div>
</template>

- 调试面板 eruda

为了方便移动端查看 log 信息和调试,开发环境引入了 eruda 调试面板的 cdn。如果你的开发环境不需要的话请在 .env.development 中修改值

# .env.development

# 开发环境启用 cdn eruda 调试工具。若不启用,将 true 修改为 false 或其他任意值即可
VITE_ENABLE_ERUDA = "true"

- 动态设置页面标题

在路由全局前置守卫中:

// src/router/index.ts
// ...
router.beforeEach((to: toRouteType, from, next) => {
  // ...
  // 页面 title
  setPageTitle(to.meta.title);
  next();
});

具体实现方法见文件 src/utils/set-page-title.ts

- 开发环境 Mock

本项目开发环境支持 mock 请求数据,在 mock 目录中可配置接口和数据,具体见文档

- vw 视口适配

使用 cnjm-postcss-px-to-viewport 进行视口适配,相关配置见项目根目录下 postcss.config.js

// postcss.config.js
module.exports = {
  plugins: {
    // 使用 cnjm-postcss-px-to-viewport 规避 postcss.plugin was deprecated 警告
    "cnjm-postcss-px-to-viewport": {
      viewportWidth: 375, // 根据设计稿设定
      minPixelValue: 1, // 最小的转换数值
      unitPrecision: 2 // 转化精度,转换后保留位数
    },
    autoprefixer: {
      overrideBrowserslist: ["Android >= 4.0", "iOS >= 7"]
    }
  }
};

- Tailwindcss 原子类框架

Tailwindcss 从 3.0 版本开始默认使用 JIT 模式,打包代码不再臃肿,结合 vite 使用非常香~ 如果你还没使用过类似的框架,Tailwindcss 首页的示例非常直观。

官方文档:https://tailwindcss.com/docs/padding

- Git 提交信息规范

项目使用 husky 规范 Git 提交信息,遵循社区主流的 Angular 规范。

feat 增加新功能
fix 修复问题/BUG
style 代码风格相关无影响运行结果的
perf 优化/性能提升
refactor 重构
revert 撤销修改
test 测试相关
docs 文档/注释
chore 依赖更新/脚手架配置修改等
workflow 工作流改进
ci 持续集成
types 类型定义文件更改
wip 开发中
// 格式
<type>(<scope>): <subject>
// 示例
feat(layout): 布局完成

- CDN 生产环境依赖

本模板生产环境默认不开启 CDN 加载依赖,如需开启生产环境加载 CDN 依赖,在根目录生产环境变量文件 .env.production 中修改 VITE_CDN_DEPS 的值为 true 重新打包即可。

鸣谢

vue-element-admin

vant-demo

vue-pure-admin

vue-vben-admin

License

MIT license.

vue3-h5-template's People

Contributors

wswmsword avatar yulimchen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue3-h5-template's Issues

请教一下怎么在每个页面给 NavBar 分别设置 title?

插槽看了还是不太懂,NavBar 和 每个 component 也没有嵌套关系

      <van-nav-bar
        fixed
        placeholder
        left-arrow
        safe-area-inset-top
        @click-left="onClickLeft"
      >
        <template #title="slotProps">
          {{ slotProps }}
        </template>
      </van-nav-bar>

      <router-view v-slot="{ Component }">
        <keep-alive :include="cachedViews">
          <component :is="Component" />
        </keep-alive>
      </router-view>

。。。。。

这算啥开箱即用模版,

移动端最常用的功能

上下拉加载解决方案呢,

转场动画呢

回到上级页面保存状态呢

全局提示呢

全局状态呢

。。。。

使用封装的Axios请求,有缓存问题

使用http请求,会将第一次请求的参数缓存进

Http.axiosConfigDefault

随后会调用

Object.assign(Http.axiosConfigDefault, paramConfig);

将参数合并,导致先请求get 后请求post时,post请求还会带上前一个get请求参数。
解决:
在请求拦截器中将默认值的参数清空

Http.axiosConfigDefault.data = {}; Http.axiosConfigDefault.params = {};

axios封装存在问题

image

如果Http.axiosConfigDefault使用的是Object.assign的话没次调用request,Http.axiosConfigDefault都会加入上一次请求的paramConfig,不断累加合并,需要改成这样const config = { ...Http.axiosConfigDefault, ...paramConfig },保证每次请求不会污染Http.axiosConfigDefault

Taliwind CSS 和 Vant使用情况下的适配问题

<img class="block w-[120px] mx-auto mb-[20px] pt-[30px]" alt="Vue logo" src="~@/assets/logo_melomini.png" />
你好,我发现在 适配vant成功的情况下,我想使用Taliwind官方的 text-base 、 text-lg等等这样的原子化class是会有冲突的,字体会很大,想问问有没有好的解决办法?

项目clone后未改动,prettier异常,保存时也不自动格式化代码,且import ** from **均标红

电脑系统:macOs

node version: v18.16.0

项目clone后未进行修改

问题1:
在其他项目中,保存时都会自动格式化代码,比如说自动修改单双引号,自动把多属性换行展示,但是在这个vue3-h5-template里均失效

问题2:
所有的import后边都标红,提示如图所示:
image

期望结果:

  1. 能在保存时自动优化代码显示,自动格式化代码
  2. import后没有标红报错(已解决但有疑问,详见tips)

tips:
问题2查了下说需要添加 "moduleResolution":"node" 属性在tsconfig.json配置文件中,加完后确实import不再标红,但是个人疑问是您那边不改变这个配置也不标红嘛?为什么我这需要额外修改这个配置
image

前端菜鸡请教,在router.beforeEach跳转OAuth连接并回调实现自动登录合适吗?

实现

企业微信客户端打开则自动跳转 OAuth 连接授权成功后 Callback Code 回来去后端换区用户 token 自动登录;非企业微信客户端跳转到 Login 页面。。

问题

  • 目前我整个逻辑写在了 router.beforeEach 里,写这里合适吗,大家都写在哪里(比如是否把跳转和自动登录写在 main.ts 中路由前?)?
  • 流程是否有问题?我的基本逻辑:判断有无 token 是否登录 > 判断是否企业微信客户端(是跳转 OAuth 连接并回调)> 判断是否有携带 Code ,自动登录。

相关代码片段

// router.beforeEach 里的判断逻辑
if (!userStore.token) {
    if (isWeWork()) {
      userStore.loginWeWork().then(isSuccess => {
        if (isSuccess) next();
      });
    } else {
      next({ path: LOGIN_URL, replace: true });
    }
  } else {
    useCachedViewStoreHook().addCachedView(to);
    next();
  }

// userStore.loginWeWork
async loginWeWork() {
      const workCode = new URLSearchParams(window.location.search).get("code");
      if (!workCode) {
        window.location.href = getOAuthUrl();
        return;
      }

      try {

        const { data }: any = await loginWeWorkApi({ code: workCode });

        this.setToken(data.token);
        this.setUserInfo(data.userInfo);
        return true;
      } catch (error) {
        return false;
      }
    }

NProgress configure导致使用Http请求时自动滚动到顶部问题

NProgress.configure({ // 动画方式 easing: "ease", // 递增进度条的速度 speed: 500, // 是否显示加载ico showSpinner: false, // 自动递增间隔 trickleSpeed: 200, // 初始化时的最小百分比 minimum: 0.3, // 加载条的父元素 parent: "#app" });
这个配置加载条的父元素为app会导致在界面在van-list滚动到底部加载完毕时自动滚动到界面顶部.或者是在界面过长时操作http请求都会导致自动滚动到界面的top

pnpm安装且编译成功,但运行时报错,识别不到@vitejs/plugin-vue

报错详细信息:

ERROR 15:39:33 [vite] Internal server error: Failed to parse source for import analysis because the content contains invalid JS syntax. Install @vitejs/plugin-vue to handle .vue files.
Plugin: vite:import-analysis
File: C://vue3-h5-template-master/vue3-h5-template-master/src/components/NavBar/index.vue:2:155
1 | /* unplugin-vue-components disabled */import { NavBar as __unplugin_components_1 } from 'vant/es';import 'vant/es/nav-bar/style/index';
2 | import __unplugin_components_0 from 'C://vue3-h5-template-master/vue3-h5-template-master/src/components/SvgIcon/index.vue';
|
^
3 | import { defineComponent as _defineComponent } from "vue";
4 | import { useDarkMode, useToggleDarkMode } from "@/hooks/useToggleDarkMode";

引入Ref 怎么会报错

使用 import { reactive, ref, Ref } from "vue"; 报错:Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/vue.js?v=f6214fab' does not provide an export named 'Ref' (at index.vue:2:25)

Vant4没有DatetimePicker,只有DatePicker,所以按需引入组件需要改为引入DatePicker

// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: vuejs/core#3399
import '@vue/runtime-core'

export {}

declare module '@vue/runtime-core' {
export interface GlobalComponents {
NavBar: typeof import('./../components/NavBar/index.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
SvgIcon: typeof import('./../components/SvgIcon/index.vue')['default']
Tabbar: typeof import('./../components/Tabbar/index.vue')['default']
VanButton: typeof import('vant/es')['Button']
VanCell: typeof import('vant/es')['Cell']
VanConfigProvider: typeof import('vant/es')['ConfigProvider']
VanDatePicker: typeof import('vant/es')['DatePicker']

VanDatetimePicker: typeof import('vant/es')['DatetimePicker']  //改为 引入 DatePicker

VanNavBar: typeof import('vant/es')['NavBar']
VanSpace: typeof import('vant/es')['Space']
VanTabbar: typeof import('vant/es')['Tabbar']
VanTabbarItem: typeof import('vant/es')['TabbarItem']

}
}

能不能出个js版本,理由进来看

前端真是太卷了,小项目根本没必要上ts,完全是增加开发成本

接单啥的,根本没有后期二开维护的说法,徒增前期开发成本

哈哈,咱们就回归本质不好吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.