I am currently developing a new app project. This is also my first time developing an app. After a period of research and investigation by the team, we decided to use the Vue3+Vant front-end component model for development. We have used Vue2 for several projects, so we decided to try using Vue3 for front-end development this time. Press Enter to search and enter the details page History Page Clear History First, create a js file. This js file mainly includes the functions of adding historical record information and deleting all historical record information. export default { // Add search home page history query record addSearchHistory(state, payload) { // When the list contains the record, delete it const index = state.searchHistoryList.indexOf(payload); if (index > -1) { state.searchHistoryList.splice(index, 1); } state.searchHistoryList.unshift(payload); // The maximum number of records in the history record is 20 const count = state.searchHistoryList.length; state.searchHistoryList.splice(20, count); }, // Clear the search history on the home page clearSearchHistory(state) { state.searchHistoryList = []; }, }; Vue Code Blocks <template> <!-- Search Box --> <search-bar @searchClick="searchClick" :placeholderValue="state.placeholderValue" :searchVal="state.searchVal"> </search-bar> <div class="search"> <!-- Search History --> <div class="history" v-if="state.isShowHistory"> <span class="proHot">Search History</span> <span class="delHotSearch" @click="delHostClick">Clear History</span> <!-- Store historical record information--> <div class="searchBtn-div"> <span v-for="(item, index) in state.historyList" :key="index" class="searchValBtn" > <van-button round size="small" @click="searchValClick(item)" >{{ item }} </van-button> </span> </div> </div> </div> </template> <script> import { onMounted, reactive, getCurrentInstance, } from 'vue'; import { Toast, Dialog } from 'vant'; import searchBar from '@/components/SearchBar.vue'; import { useRouter } from 'vue-router'; import { useStore } from 'vuex'; export default { components: searchBar, }, setup() { const router = useRouter(); const store = useStore(); const { proxy } = getCurrentInstance(); const state = reactive({ isShowHistory: '', // Whether to display history searchVal: '', // Search keyword placeholderValue: 'Search for products/information/standards/ingredients/enterprises', historyList: [], // historical search data}); // Enter to search const searchClick = (val) => { store.commit('addSearchHistory', val); // router.push({ path: '/search-detail', query: { searchVal: val } }); }; // Clear history const delHostClick = async () => { Dialog.confirm({ message: 'Are you sure you want to delete your search history? ', }).then(() => { store.commit('clearSearchHistory', store); state.isShowHistory = false; Toast({ message: 'Deleted successfully', position: 'bottom', }); }); }; // Initialize and obtain historical search record information onMounted(async () => { // Get historical search information state.historyList = store.state.searchHistoryList; // Determine whether to initialize the display history search if (state.historyList.length > 0) { state.isShowHistory = true; } else { state.isShowHistory = false; } }); return { state, searchClick, delHostClick, }; }, }; </script> <style lang="less" scoped> </style> If you copy and paste the Vue code directly, you may not be able to use it directly, because a lot of business codes have been deleted, and the remaining ones are mainly the codes for historical search records. There are three main focuses: Introducing useStore import { useStore } from 'vuex'; const store = useStore(); Initialize search history // Initialize and obtain historical search record information // Each time this page is loaded, this method will be called first to get the latest information onMounted(async () => { // Get historical search information state.historyList = store.state.searchHistoryList; // Determine whether to initialize the display history search if (state.historyList.length > 0) { state.isShowHistory = true; } else { state.isShowHistory = false; } }) The search box triggers a search event and stores the search information in the Store // The child component emits an event, and the parent component calls const searchClick = (val) => { // Put the search value into the history store.commit('addSearchHistory', val); // Route redirection can be ignored // router.push({ path: '/search-detail', query: { searchVal: val } }); }; Clear History // Clear history const delHostClick = async () => { Dialog.confirm({ message: 'Are you sure you want to delete your search history? ', }).then(() => { // Clear history informationstore.commit('clearSearchHistory', store); state.isShowHistory = false; Toast({ message: 'Deleted successfully', position: 'bottom', }); }); }; The above is the details of using Vue3+Vant component to implement the App search history function. For more information about vue search history, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Open the Windows server port (take port 8080 as an example)
>>: VMware 15.5 version installation Windows_Server_2008_R2 system tutorial diagram
1. Introduction Since pictures take up a lot of s...
Installation & Configuration The official web...
Table of contents Preface Summary of the principl...
Table of contents 1 The role of Apache 2 Apache I...
【question】 We have an HP server. When the SSD wri...
A common development need is that we want to coll...
1 Cause After the project migrated the database a...
This reading note mainly records the operations r...
Let’s take a look at what kind of charging animat...
Flex layout is also called elastic layout. Any co...
Preface Only Innodb and MyISAM storage engines ca...
The relationship between Javascript and DOM is ve...
Table of contents 1. Truncate operation 1.1 What ...
Using Dockerfile allows users to create custom im...
Inline format <colgroup>...</colgroup>...