vue開發(fā)商城項(xiàng)目步驟和配置全過程
初始化項(xiàng)目
使用Vue CLI創(chuàng)建項(xiàng)目,確保已安裝Node.js和npm/yarn:
vue create mall-project
選擇默認(rèn)配置或手動(dòng)配置(Babel、Router、Vuex等)。
如需使用Vite:
npm init vue@latest mall-project
安裝核心依賴
進(jìn)入項(xiàng)目目錄后安裝必要依賴:
npm install axios vue-router@4 pinia element-plus sass
axios:處理HTTP請(qǐng)求vue-router:路由管理pinia:狀態(tài)管理(替代Vuex)element-plus:UI組件庫sass:CSS預(yù)處理器
目錄結(jié)構(gòu)配置
典型商城項(xiàng)目目錄結(jié)構(gòu):
src/ ├── assets/ # 靜態(tài)資源 ├── components/ # 公共組件 ├── views/ # 頁面級(jí)組件 ├── stores/ # Pinia狀態(tài)管理 ├── router/ # 路由配置 ├── utils/ # 工具函數(shù) ├── styles/ # 全局樣式 ├── api/ # 接口封裝 └── App.vue # 根組件
路由配置示例
在router/index.js中配置基礎(chǔ)路由:
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{
path: '/',
component: () => import('@/views/Home.vue')
},
{
path: '/product/:id',
component: () => import('@/views/ProductDetail.vue')
},
{
path: '/cart',
component: () => import('@/views/Cart.vue')
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
Pinia狀態(tài)管理
創(chuàng)建購物車store(stores/cart.js):
import { defineStore } from 'pinia'
export const useCartStore = defineStore('cart', {
state: () => ({
items: []
}),
actions: {
addItem(product) {
const existItem = this.items.find(item => item.id === product.id)
existItem ? existItem.quantity++ : this.items.push({ ...product, quantity: 1 })
}
},
getters: {
totalPrice: (state) => state.items.reduce((total, item) => total + item.price * item.quantity, 0)
}
})
接口封裝
在api/目錄下創(chuàng)建請(qǐng)求封裝(如api/product.js):
import axios from 'axios'
const instance = axios.create({
baseURL: 'https://your-api-endpoint.com'
})
export const getProducts = () => instance.get('/products')
export const getProductDetail = (id) => instance.get(`/products/${id}`)
全局樣式配置
在styles/目錄下:
variables.scss:定義SCSS變量mixins.scss:定義復(fù)用樣式main.scss:導(dǎo)入全局樣式
在main.js中引入:
import '@/styles/main.scss'
頁面組件示例
商品列表組件(views/Home.vue):
<template>
<div class="product-list">
<el-card v-for="product in products" :key="product.id">
<img :src="product.image" />
<h3>{{ product.name }}</h3>
<p>¥{{ product.price }}</p>
<el-button @click="addToCart(product)">加入購物車</el-button>
</el-card>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { getProducts } from '@/api/product'
import { useCartStore } from '@/stores/cart'
const cartStore = useCartStore()
const products = ref([])
const fetchProducts = async () => {
const res = await getProducts()
products.value = res.data
}
const addToCart = (product) => {
cartStore.addItem(product)
}
fetchProducts()
</script>
項(xiàng)目配置調(diào)優(yōu)
在vite.config.js或vue.config.js中添加優(yōu)化配置:
// vite示例
export default defineConfig({
css: {
preprocessorOptions: {
scss: {
additionalData: `@import "@/styles/variables.scss";`
}
}
},
server: {
proxy: {
'/api': {
target: 'http://your-api-server.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
}
})
構(gòu)建與部署
生產(chǎn)環(huán)境構(gòu)建命令:
npm run build
部署到Nginx需配置:
server {
listen 80;
location / {
root /path/to/dist;
try_files $uri $uri/ /index.html;
}
}
注意事項(xiàng)
- 商品詳情頁需處理SEO,可使用vue-meta或@unhead/vue
- 支付流程建議使用第三方SDK(如支付寶/微信支付官方JSAPI)
- 圖片懶加載使用
v-lazy指令或Intersection Observer API - 接口安全需配置JWT鑒權(quán)或CSRF防護(hù)
- 移動(dòng)端適配推薦使用vw/vh單位或postcss-px-to-viewport插件
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js 多項(xiàng)目同端口部署實(shí)戰(zhàn)指南(上下文路徑配置指南)
上下文路徑是指 Web應(yīng)用在服務(wù)器上的部署路徑前綴,通過設(shè)置不同的上下文路徑,多個(gè)應(yīng)用可以共享同一個(gè)IP和端口,通過URL路徑來區(qū)分,本文將從前端Vue.js配置到后端服務(wù)器部署,詳細(xì)介紹完整的實(shí)現(xiàn)方案,感興趣的朋友跟隨小編一起看看吧2026-02-02
vue實(shí)現(xiàn)前端excel導(dǎo)出的三種方法實(shí)現(xiàn)與對(duì)比總結(jié)
在Vue項(xiàng)目中實(shí)現(xiàn)前端Excel導(dǎo)出功能,主要有三種主流方案,各有特點(diǎn),下面小編就為大家詳細(xì)介紹一下這三種方法的實(shí)現(xiàn),大家可以根據(jù)需要進(jìn)行選擇2025-10-10
Vue生產(chǎn)環(huán)境如何自動(dòng)屏蔽console
這篇文章主要介紹了Vue生產(chǎn)環(huán)境如何自動(dòng)屏蔽console問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Vue3?中路由Vue?Router?的使用實(shí)例詳解
vue-router是vue.js官方給出的路由解決方案,能夠輕松的管理SPA項(xiàng)目中組件的切換,這篇文章主要介紹了Vue3?中路由Vue?Router?的使用,需要的朋友可以參考下2023-02-02
vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)PC端錄音功能的實(shí)例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
vue2.0中click點(diǎn)擊當(dāng)前l(fā)i實(shí)現(xiàn)動(dòng)態(tài)切換class
本篇文章主要介紹了vue2.0中click點(diǎn)擊當(dāng)前l(fā)i實(shí)現(xiàn)動(dòng)態(tài)切換class ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

