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輸入框狀態(tài)切換&自動(dòng)獲取輸入框焦點(diǎn)的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue輸入框狀態(tài)切換&自動(dòng)獲取輸入框焦點(diǎn)的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
vue antd的from表單中驗(yàn)證rules中type的坑記錄
這篇文章主要介紹了vue antd的from表單中驗(yàn)證rules中type的坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue項(xiàng)目首屏性能優(yōu)化組件實(shí)戰(zhàn)指南
Vue眾所周知是一個(gè)輕量級(jí)的框架,源碼僅僅為72.9KB,但是也有它自己的缺點(diǎn),就是首屏加載會(huì)比較慢,這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目首屏性能優(yōu)化組件的相關(guān)資料,需要的朋友可以參考下2021-11-11
Vue.js實(shí)現(xiàn)微信過渡動(dòng)畫左右切換效果
這篇文章主要給大家介紹了利用Vue.js仿微信過渡動(dòng)畫左右切換效果的相關(guān)資料,需要用到的技術(shù)棧是Vue+Vuex。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
詳解vue數(shù)據(jù)渲染出現(xiàn)閃爍問題
本篇文章主要介紹了vue數(shù)據(jù)渲染出現(xiàn)閃爍問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

