vue開發(fā)商城項目步驟和配置全過程
初始化項目
使用Vue CLI創(chuàng)建項目,確保已安裝Node.js和npm/yarn:
vue create mall-project
選擇默認配置或手動配置(Babel、Router、Vuex等)。
如需使用Vite:
npm init vue@latest mall-project
安裝核心依賴
進入項目目錄后安裝必要依賴:
npm install axios vue-router@4 pinia element-plus sass
axios:處理HTTP請求vue-router:路由管理pinia:狀態(tài)管理(替代Vuex)element-plus:UI組件庫sass:CSS預處理器
目錄結構配置
典型商城項目目錄結構:
src/ ├── assets/ # 靜態(tài)資源 ├── components/ # 公共組件 ├── views/ # 頁面級組件 ├── stores/ # Pinia狀態(tài)管理 ├── router/ # 路由配置 ├── utils/ # 工具函數(shù) ├── styles/ # 全局樣式 ├── api/ # 接口封裝 └── App.vue # 根組件
路由配置示例
在router/index.js中配置基礎路由:
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)建請求封裝(如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:定義復用樣式main.scss:導入全局樣式
在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>
項目配置調優(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/, '')
}
}
}
})
構建與部署
生產(chǎn)環(huán)境構建命令:
npm run build
部署到Nginx需配置:
server {
listen 80;
location / {
root /path/to/dist;
try_files $uri $uri/ /index.html;
}
}
注意事項
- 商品詳情頁需處理SEO,可使用vue-meta或@unhead/vue
- 支付流程建議使用第三方SDK(如支付寶/微信支付官方JSAPI)
- 圖片懶加載使用
v-lazy指令或Intersection Observer API - 接口安全需配置JWT鑒權或CSRF防護
- 移動端適配推薦使用vw/vh單位或postcss-px-to-viewport插件
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn)方法
這篇文章主要介紹了Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
vue antd的from表單中驗證rules中type的坑記錄
這篇文章主要介紹了vue antd的from表單中驗證rules中type的坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
詳解vue數(shù)據(jù)渲染出現(xiàn)閃爍問題
本篇文章主要介紹了vue數(shù)據(jù)渲染出現(xiàn)閃爍問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

