# vue-jd-h5 **Repository Path**: gankai/vue-jd-h5 ## Basic Information - **Project Name**: vue-jd-h5 - **Description**: 基于vue2.6+ ,vue3.0.0-beta.1,vue3, vue-cli3,mockjs,仿京东淘宝的,移动端H5电商平台! - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: vue-next - **Homepage**: https://geekskai.github.io/vue3-jd-h5/ - **GVP Project**: No ## Statistics - **Stars**: 15 - **Forks**: 9 - **Created**: 2020-03-04 - **Last Updated**: 2025-05-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
# vue3-jd-h5

Twitter GitHub issues GitHub forks GitHub stars

English| 简体中文

## 项目介绍 `vue3-jd-h5`是一个电商 H5 页面前端项目,基于**Vue 3.0.0** + **Vant 3.0.0** 实现,主要包括首页、分类页面、我的页面、购物车等。 📖 本地线下代码**vue2.6**在分支demo中,使用**mockjs**数据进行开发,效果图请点击 🔗这里 ⚠️master 分支是线上生产环境代码,因为部分后台接口已经挂了 😫,可能无法看到实际效果。 📌 本项目还有很多不足之处,如果有想为此做贡献的伙伴,也欢迎给我们提出 PR,或者 issue ; 🔑 本项目是免费开源的,如果有伙伴想要在次基础上进行二次开发,可以 clone 或者 fork 整个仓库,如果能帮助到您,我将感到非常高兴,如果您觉得这个项目不错还请给个 start!🙏 💥 2024 年 Vue3.x ,Vite5.x, Vant,TypeScript 最新版本更新分支:👉 vue3-vite-vant-ts 🎉 如果您乐意捐赠,我们也非常欢迎,可以通过以下渠道进行捐款:

https://user-images.githubusercontent.com/37830362/173991179-71272ccb-bf69-441f-8f15-46e8da403d2a.mov ## vue3 搭建步骤 1. 首先,在本地选择一个文件,将代码 clone 到本地: ```bash git clone https://github.com/GitHubGanKai/vue-jd-h5.git ``` 2. 查看所有分支: ```bash gankaideMacBook-Pro:vue-jd-h5 gankai$ git branch -a demo vue-next dev feature gh-pages * master remotes/origin/HEAD -> origin/master remotes/origin/demo remotes/origin/vue-next remotes/origin/dev remotes/origin/feature remotes/origin/gh-pages remotes/origin/master ``` 3. 切换到分支**vue-next**开始进行开发! 4. 在 IDEA 命令行中运行命令:npm install,下载相关依赖; 5. 🔧 开发环境 在 IDEA 命令行中运行命令:`npm run dev`,运行项目; 6. 📦 在 IDEA 命令行中运行命令:`npm run dll:build`,打包项目,📱 手机扫描下面 👇 二维码进行查看!
## 项目的初始化 💡 如果你在安装包的时候速度比较慢,那是因为 NPM 服务器在国外,这里给大家推荐一个可以随时切换 NPM 镜像的工具 👉[NRM](https://www.npmjs.com/package/nrm),有时候,我们开发的时候,为了加快安装包的安装速度,我们需要切换镜像源为国内的,但是,如果需要发布一些自己的组件到 NPM 的时候,又要来回切换回来,有了这个我们就方便多了!使用`$ npm install -g nrm`全局安装,然后,可以使用`nrm ls` 查看所有镜像: ```bash gankaideMacBook-Pro:~ gankai$ nrm ls npm -------- https://registry.npmjs.org/ * yarn ------- https://registry.yarnpkg.com/ cnpm ------- http://r.cnpmjs.org/ taobao ----- https://registry.npm.taobao.org/ nj --------- https://registry.nodejitsu.com/ npmMirror -- https://skimdb.npmjs.com/registry/ edunpm ----- http://registry.enpmjs.org/ ``` 如果需要使用淘宝镜像,执行: `nrm use taobao` 可以随时切换源,当然了还有一个 npm 包版本管理工具[nvm](https://github.com/nvm-sh/nvm),主要是管理包版本的,如果有兴趣的小伙伴,可以自己去了解一下,这里就不啰嗦了 😊! ### 安装 进入刚才 clone 下来的项目根目录,安装相关依赖,体验 vue3 新特性。 `npm`安装: ```javascript npm install ``` `yarn`安装: ```javascript yarn; ``` **CDN** ```html ``` ### 使用 在入口文件`main.js`中: ```javascript import Vue from "vue"; import VueCompositionApi from "@vue/composition-api"; Vue.use(VueCompositionApi); ``` 安装插件后,您就可以使用新的 [Composition API](https://vue-composition-api-rfc.netlify.com/) 来开发组件了。 ⚠️ 目前 vue 官方为 vue-cli 提供了一个插件[vue-cli-plugin-vue-next](https://github.com/vuejs/vue-cli-plugin-vue-next),你也可以直接在项目中直接添加最新的版本! ```bash # in an existing Vue CLI project vue add vue-next ```
如果有想从零开始体验新版本的小伙伴可以采用这种方法进行安装,由于我们这个项目有依赖第三方库,如果全局安装,整个项目第三方 UI 库都无法运行!所以我们还是选择采用安装`@vue/composition-api`来进行体验,从而慢慢过渡到 vue3 最新版本
## Vue 3.0 Composition-API 基本特性体验 ### setup 函数 `setup()` 函数是 vue3 中专门为组件提供的新属性,相当于 2.x 版本中的`created`函数,之前版本的组件逻辑选项,现在都统一放在这个函数中处理。它为我们使用 vue3 的 `Composition API` 新特性提供了统一的入口,**setup** 函数会在相对于 2.x 来说,会在 **beforeCreate** 之后、**created** 之前执行!具体可以参考如下: | vue2.x | vue3 | | :--------------: | :-------------: | | ~~beforeCreate~~ | setup(替代) | | ~~created~~ | setup(替代) | | beforeMount | onBeforeMount | | mounted | onMounted | | beforeUpdate | onBeforeUpdate | | updated | onUpdated | | beforeDestroy | onBeforeUnmount | | destroyed | onUnmounted | | errorCaptured | onErrorCaptured | ### 新钩子 除了 2.x 生命周期等效项之外,Composition API 还提供了以下 debug hooks: - `onRenderTracked` - `onRenderTriggered` 两个钩子都收到`DebuggerEvent`类似于`onTrack`和`onTrigger`观察者的选项: ```javascript export default { onRenderTriggered(e) { debugger; // inspect which dependency is causing the component to re-render }, }; ``` ### 依赖注入 `provide`和`inject`启用类似于 2.x `provide/inject`选项的依赖项注入。两者都只能在`setup()`当前活动实例期间调用。 ```javascript import { provide, inject } from "@vue/composition-api"; const ThemeSymbol = Symbol(); const Ancestor = { setup() { provide(ThemeSymbol, "dark"); }, }; const Descendent = { setup() { const theme = inject(ThemeSymbol, "light" /* optional default value */); return { theme, }; }, }; ``` `inject`接受可选的默认值作为第二个参数。如果未提供默认值,并且在 Provide 上下文中找不到该属性,则`inject`返回`undefined`。 **注入响应式数据** 为了保持提供的值和注入的值之间的响应式,可以使用`ref` ```javascript // 在父组件中 const themeRef = ref("dark"); provide(ThemeSymbol, themeRef); // 组件中 const theme = inject(ThemeSymbol, ref("light")); watchEffect(() => { console.log(`theme set to: ${theme.value}`); }); ``` 1. 因为`setup`函数接收 2 个形参,第一个是`initProps`,即父组件传送过来的值!,第二个形参是一个**上下文对象** `setupContext`,这个对象的主要属性有 : ```javascript attrs: Object; // 等同 vue 2.x中的 this.$attrs emit: ƒ(); // 等同 this.$emit() isServer: false; // 是否是服务端渲染 listeners: Object; // 等同 vue2.x中的this.$listeners parent: VueComponent; // 等同 vue2.x中的this.$parent refs: Object; // 等同 vue2.x中的this.$refs root: Vue; // 这个root是我们在main.js中,使用newVue()的时候,返回的全局唯一的实例对象,注意别和单文件组件中的this混淆了 slots: { } // 等同 vue2.x中的this.$slots ssrContext: { } // 服务端渲染相关 ``` ⚠️**注意**:在 `setup()` 函数中无法访问到 `this`的,不管这个`this`指的是全局的的 vue 对象(即:在 main.js 中使用 new 生成的那个全局的 vue 实例对象),还是指单文件组件的对象。 但是,如果我们想要访问当前组件的实例对象,那该怎么办呢?我们可以引入`getCurrentInstance`这个 api,返回值就是当前组件的实例! ```javascript import { computed, getCurrentInstance } from "@vue/composition-api"; export default { name: "svg-icon", props: { iconClass: { type: String, required: true }, className: { type: String } }, setup(initProps,setupContext) { const { ctx } = getCurrentInstance(); const iconName = computed(() => { return `#icon-${initProps.iconClass}`; }); const svgClass = computed(() => { if (initProps.className) { return "svg-icon " + initProps.className; } else { return "svg-icon"; } }); return { iconName, svgClass }; } }; ``` ### Ref 自动展开(unwrap) `ref()` 函数用来根据给定的值创建一个**响应式**的**数据对象**,`ref()` 函数调用的返回值是一个被包装后的对象(RefImpl),这个对象上只有一个 `.value` 属性,如果我们在`setup`函数中,想要访问的对象的值,可以通过`.value`来获取,但是如果是在`