如何使用 Vue 實現仿京東搜尋頁面?
Vue 是當下非常流行的前端框架之一,它可以幫助我們快速建立高效且美觀的使用者介面。在本文中,我們將介紹如何使用 Vue 實現仿京東搜尋頁面。
首先,我們需要準備以下工具和技術:
- Vue CLI:用於快速初始化一個 Vue 專案。
- axios:用於傳送 HTTP 請求和處理回應。
- Vuex:用於管理應用程式的狀態。
- Element UI:用於提供各種 UI 元件。
- 初始化Vue 專案
使用Vue CLI 初始化一個新的專案非常簡單,只需要在命令列中執行以下命令:
vue create jd-search
這個指令會在目前目錄下建立一個名為jd-search
的項目,並自動安裝所需的依賴項。
- 新增Element UI
安裝Element UI 非常簡單,只需要在命令列中執行以下命令:
npm install element-ui
安裝完成後,在main.js
中引入Element UI:
import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI)
- 建立搜尋元件
在src/components
目錄下創建一個名為Search.vue
的元件。這個元件包含一個輸入框和一個搜尋按鈕,程式碼如下:
<template> <div class="search"> <el-input v-model="keyword" placeholder="请输入关键词" class="search-input" @keyup.enter.native="search" /> <el-button type="primary" icon="el-icon-search" class="search-btn" @click="search" /> </div> </template> <script> export default { data() { return { keyword: '', } }, methods: { search() { this.$emit('search', this.keyword); } }, } </script> <style scoped> .search { display: flex; flex-direction: row; align-items: center; justify-content: center; } .search-input { width: 500px; margin-right: 20px; } .search-btn { width: 80px; } </style>
這個元件包含一個 keyword
資料屬性,用來保存使用者輸入的關鍵字。當使用者點擊搜尋按鈕或按下回車鍵時,會觸發 search
方法,將目前的 keyword
值作為參數傳遞給父元件。
- 建立商品清單元件
在 src/components
目錄下建立一個名為 ProductList.vue
的元件。這個元件會顯示搜尋結果的商品列表,程式碼如下:
<template> <div class="product-list"> <el-card v-for="product in products" :key="product.id"> <div slot="header" class="product-header"> <h3>{{ product.title }}</h3> <span class="product-price">{{ product.price }}</span> </div> <div> <img :src="product.image" class="product-image" /> </div> <div> {{ product.description }} </div> </el-card> </div> </template> <script> export default { props: { products: { type: Array, default: () => [], }, }, } </script> <style scoped> .product-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); grid-gap: 20px; } .product-header { display: flex; flex-direction: row; align-items: center; justify-content: space-between; } .product-price { font-size: 18px; } </style>
這個元件接收一個名為 products
的屬性,用來顯示搜尋結果。它使用 Element UI 的 el-card
和 el-image
元件顯示商品列表,並使用 CSS Grid 實現自適應佈局。
- 建立狀態管理模組
使用 Vuex 管理應用程式的狀態非常方便。在 src/store
目錄下建立一個名為 search.js
的模組。這個模組包含以下狀態、操作和 getters:
const state = { keyword: '', products: [], }; const mutations = { updateKeyword(state, keyword) { state.keyword = keyword; }, updateProducts(state, products) { state.products = products; }, }; const actions = { async search({ commit }, keyword) { const response = await this.$axios.get('/api/search', { params: { keyword }, }); commit('updateProducts', response.data); }, }; const getters = {}; export default { namespaced: true, state, mutations, actions, getters, };
這個模組包含一個名為 search
的非同步操作,用於發送搜尋請求並更新搜尋結果。它還包含一個名為 keyword
的狀態和一個名為 products
的狀態,用於保存使用者輸入的關鍵字和搜尋結果。
- 建立搜尋頁面
建立一個名為SearchPage.vue
的頁面,它會包含Search
和ProductList
元件,並透過Vuex 管理它們之間的互動。程式碼如下:
<template> <div class="search-page"> <search @search="search" /> <product-list :products="products" /> </div> </template> <script> import Search from '@/components/Search'; import ProductList from '@/components/ProductList'; import { mapState, mapActions } from 'vuex'; export default { components: { Search, ProductList }, computed: { ...mapState('search', ['products']), }, methods: { ...mapActions('search', ['search']), }, } </script> <style scoped> .search-page { display: flex; flex-direction: column; align-items: center; padding: 20px; } </style>
這個頁麵包含Search
和ProductList
元件,並使用Vuex 的mapState
和mapActions
映射products
屬性和search
操作。當使用者輸入關鍵字並點擊搜尋按鈕或按下回車鍵時,會觸發 search
操作,從後端 API 取得搜尋結果,並更新 Vuex 中的 products
狀態。
- 傳送搜尋請求
在src/main.js
中設定Axios,程式碼如下:
import axios from 'axios' import VueAxios from 'vue-axios' Vue.use(VueAxios, axios) axios.defaults.baseURL = 'http://localhost:3000'
這個設定使得我們可以在應用程式中使用$axios
物件發送HTTP 請求。現在,我們可以在 search
操作中發送搜尋請求了。
至此,我們已經完成了仿京東搜尋頁面的實作。這個頁面使用了 Vue、Element UI、Axios 和 Vuex 等技術,並遵循了現代化的單頁應用程式的最佳實踐。
以上是如何使用 Vue 實現仿京東搜尋頁面?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

可以通過以下步驟為 Vue 按鈕添加函數:將 HTML 模板中的按鈕綁定到一個方法。在 Vue 實例中定義該方法並編寫函數邏輯。

Vue.js 中的 watch 選項允許開發者監聽特定數據的變化。當數據發生變化時,watch 會觸發一個回調函數,用於執行更新視圖或其他任務。其配置選項包括 immediate,用於指定是否立即執行回調,以及 deep,用於指定是否遞歸監聽對像或數組的更改。

Vue.js 返回上一頁有四種方法:$router.go(-1)$router.back()使用 <router-link to="/"> 組件window.history.back(),方法選擇取決於場景。

Vue 多頁面開發是一種使用 Vue.js 框架構建應用程序的方法,其中應用程序被劃分為獨立的頁面:代碼維護性:將應用程序拆分為多個頁面可以使代碼更易於管理和維護。模塊化:每個頁面都可以作為獨立的模塊,便於重用和替換。路由簡單:頁面之間的導航可以通過簡單的路由配置來管理。 SEO 優化:每個頁面都有自己的 URL,這有助於搜索引擎優化。

NetflixusesAcustomFrameworkcalled“ Gibbon” BuiltonReact,notReactorVuedIrectly.1)TeamSperience:selectBasedonFamiliarity.2)ProjectComplexity:vueforsimplerprojects:reactforforforproproject,reactforforforcompleplexones.3)cocatizationneedneeds:reactoffipicatizationneedneedneedneedneedneeds:reactoffersizationneedneedneedneedneeds:reactoffersizatization needefersmoreflexibleise.4)

Vue.js 遍歷數組和對像有三種常見方法:v-for 指令用於遍歷每個元素並渲染模板;v-bind 指令可與 v-for 一起使用,為每個元素動態設置屬性值;.map 方法可將數組元素轉換為新數組。

在 Vue.js 中引用 JS 文件的方法有三種:直接使用 <script> 標籤指定路徑;利用 mounted() 生命週期鉤子動態導入;通過 Vuex 狀態管理庫進行導入。
