vue之this.$router.push頁(yè)面刷新問(wèn)題
跳轉(zhuǎn)當(dāng)前路由(路由參數(shù)有變化)(路由參數(shù)無(wú)變化此方法無(wú)效)
使用this.$router.push
進(jìn)行跳轉(zhuǎn)時(shí),如果是跳轉(zhuǎn)當(dāng)前路由(路由參數(shù)有變化),可在 App.vue 里的 router-view
標(biāo)簽設(shè)置 key
值。
或使用監(jiān)聽(tīng)器 watch
<template> <div id="app"> <DataSearch/> <keep-alive :exclude="exclude"> <!-- 通過(guò)key設(shè)置頁(yè)面刷新 規(guī)矩 $route.fullPath 得到路由(包含帶?的參數(shù)) :key="$route.fullPath" 如果路由改變就刷新 --> <!-- <router-view :key="$route.fullPath"></router-view>--> <router-view :key="$route.fullPath"></router-view> </keep-alive> </div> </template>
跳轉(zhuǎn)當(dāng)前路由(路由參數(shù)也無(wú)變化)
可以創(chuàng)建一個(gè)中轉(zhuǎn) vue 界面,詳見(jiàn)代碼:
首先 我們來(lái)看主要功能代碼:
假設(shè)我現(xiàn)在想實(shí)現(xiàn):在 datasearch.vue 中設(shè)置一個(gè)搜索按鈕,點(diǎn)擊搜索就跳轉(zhuǎn)至 datadisplay.vue 頁(yè)面,并且 datadisplay.vue 頁(yè)面會(huì)重新刷新渲染(不管路由是否變化)
datasearch.vue 中
<template> <div style="text-align: center;"> <el-autocomplete class="input-with-select" style="width: 80%;" popper-class="my-autocomplete" v-model="state" :fetch-suggestions="querySearch" placeholder="請(qǐng)輸入內(nèi)容" value-key="value" @change="sousuo" > <!-- 使用 value-key 屬性,可以指定任意列作為顯示用的 --> <!-- 自定義模板 --> <!-- 比如多個(gè)顯示 --> <!-- <template slot-scope="{ item }">--> <!-- <div class="name">{{ item.value }}</div>--> <!-- <span class="addr">{{ item.address }}</span>--> <!-- </template>--> <el-button slot="append" icon="el-icon-search" @click="sousuo" >搜索</el-button> </el-autocomplete> </div> </template>
<script> export default { name: 'DataSearch', data() { return { state: '', content: [], fullPath: '', }; }, methods: { querySearch(queryString, cb) { var restaurants = this.loadAll(); var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants; // 調(diào)用 callback 返回建議列表的數(shù)據(jù) cb(results); }, createFilter(queryString) { return (restaurant) => { // restaurant.value.toLowerCase().indexOf(queryString.toLowerCase())如果元素存在列表中返回下標(biāo),否則返回-1 console.log(restaurant.value.toLowerCase().indexOf(queryString.toLowerCase())); return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1); // 如果大于-1則代表有這個(gè)關(guān)鍵字 }; }, loadAll() { // console.log(this.content); return this.content }, sousuo(){ this.$router.push({ path: '/zhongzhuan', query: {state: this.state}, }); }, } } </script>
App.vue 中
<template> <div id="app"> <DataSearch/> <!-- 這里必須要通過(guò) :exclude 設(shè)置 緩存黑名單 否則跳轉(zhuǎn)會(huì)出問(wèn)題 --> <!-- 黑名單中要包含 中轉(zhuǎn)的 vue 名 和中轉(zhuǎn)后的 vue 名 --> <keep-alive :exclude="exclude"> <!-- 通過(guò)key設(shè)置頁(yè)面刷新 規(guī)矩 $route.fullPath 得到路由(包含帶?的參數(shù)) :key="$route.fullPath" 如果路由改變就刷新 --> <!-- <router-view :key="$route.fullPath"></router-view>--> <!-- <router-view :key="$route.fullPath"></router-view>--> <!-- 這里設(shè)置或不設(shè)置 key 都可以 --> <router-view></router-view> </keep-alive> </div> </template>
<script> import DataSearch from './components/datasearch.vue' export default { name: 'App', components: { DataSearch }, data() { return { exclude: ['datadisplay', 'zhongzhuan'], } }, } </script>
zhongzhuan.vue 中
<template> <div></div> </template>
<script> export default { // 用來(lái)中轉(zhuǎn),避免路由不變時(shí) 頁(yè)面不刷新 name: "zhongzhuan", created() { this.pushUrl() }, methods: { getData(){ return this.$route.query.state }, pushUrl(){ this.$router.push({ path: '/datadisplay', query: {state: this.getData()}, // 傳遞參數(shù),放在url?后面的 }) } }, } </script>
datadisplay.vue 中
<template> <div> <p>content:{{ content }}</p> </div> </template>
<script> export default { name: "datadisplay", data(){ return { content: '123', } }, created() { this.getData() }, methods: { getData(){ //this.$router 實(shí)際上就是全局路由對(duì)象任何頁(yè)面都可以調(diào)用 push(), go()等方法; // this.$route 表示當(dāng)前正在用于跳轉(zhuǎn)的路由器對(duì)象,可以調(diào)用其name、path、query、params等屬性。 // 應(yīng)此需要接受路由參數(shù)時(shí),要用this.$route,發(fā)送跳轉(zhuǎn)路由時(shí)要用this.$router console.log(this.$route); this.content = this.$route.query; } } } </script> <style scoped> </style>
對(duì)應(yīng)代碼實(shí)現(xiàn)圖片:
- 第一次點(diǎn)擊
- 第二次點(diǎn)擊
- 第三次點(diǎn)擊
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue?跳轉(zhuǎn)頁(yè)面$router.resolve和$router.push案例詳解
- vue中的this.$router.push()路由傳值方式
- Vue中$router.push()路由切換及如何傳參和獲取參數(shù)
- vue中this.$router.push()路由傳值和獲取的兩種常見(jiàn)方法匯總
- vue如何通過(guò)$router.push傳參數(shù)
- Vue this.$router.push(參數(shù))實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)操作
- vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法
- 對(duì)vue2.0中.vue文件頁(yè)面跳轉(zhuǎn)之.$router.push的用法詳解
- Vue $router.push打開(kāi)新窗口的實(shí)現(xiàn)方法
相關(guān)文章
Vue使用三方工具vueUse實(shí)現(xiàn)虛擬列表
其實(shí)采用vueUse中的useVirtualList方法同樣可以實(shí)現(xiàn)虛擬列表,這篇文章小編就來(lái)和大家詳細(xì)介紹一下如何使用vueUse實(shí)現(xiàn)簡(jiǎn)單的虛擬列表效果吧2024-04-04vue+element+Java實(shí)現(xiàn)批量刪除功能
這篇文章主要介紹了vue+element+Java實(shí)現(xiàn)批量刪除功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04從Vue到Postman全面驗(yàn)證API接口跨域問(wèn)題解決
我們都知道跨域是同源策略導(dǎo)致的,域名不同、協(xié)議不同、端口號(hào)不同任意一種情況都會(huì)導(dǎo)致跨域,這篇文章主要介紹了從Vue到Postman全面驗(yàn)證API接口跨域問(wèn)題,需要的朋友可以參考下2024-08-08Vue2仿淘寶實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Vue2仿淘寶實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Vue實(shí)現(xiàn)當(dāng)訪問(wèn)的路由不存在時(shí)跳轉(zhuǎn)到404頁(yè)面的方法詳解
在 Vue3 中,可以使用 Vue Router 實(shí)現(xiàn)跳轉(zhuǎn)到 404 頁(yè)面,即當(dāng)用戶訪問(wèn)一個(gè)不存在路由時(shí),系統(tǒng)會(huì)默認(rèn)跳轉(zhuǎn)到 404 頁(yè)面,本文給大家介紹了一個(gè)簡(jiǎn)單的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-12-12在vue中實(shí)現(xiàn)清除echarts上次保留的數(shù)據(jù)(親測(cè)有效)
這篇文章主要介紹了在vue中實(shí)現(xiàn)清除echarts上次保留的數(shù)據(jù)(親測(cè)有效),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09使用Vue綁定class和style樣式的幾種寫(xiě)法總結(jié)
這篇文章主要介紹了使用Vue綁定class和style樣式的幾種寫(xiě)法,文章通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07Vue多種方法實(shí)現(xiàn)表頭和首列固定的示例代碼
本篇文章主要介紹了Vue多種方法實(shí)現(xiàn)表頭和首列固定的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02