vue之this.$router.push頁面刷新問題
更新時間:2023年12月04日 14:46:09 作者:兇呆呆
這篇文章主要介紹了vue之this.$router.push頁面刷新問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
跳轉當前路由(路由參數有變化)(路由參數無變化此方法無效)
使用this.$router.push
進行跳轉時,如果是跳轉當前路由(路由參數有變化),可在 App.vue 里的 router-view
標簽設置 key
值。
或使用監(jiān)聽器 watch
<template> <div id="app"> <DataSearch/> <keep-alive :exclude="exclude"> <!-- 通過key設置頁面刷新 規(guī)矩 $route.fullPath 得到路由(包含帶?的參數) :key="$route.fullPath" 如果路由改變就刷新 --> <!-- <router-view :key="$route.fullPath"></router-view>--> <router-view :key="$route.fullPath"></router-view> </keep-alive> </div> </template>
跳轉當前路由(路由參數也無變化)
可以創(chuàng)建一個中轉 vue 界面,詳見代碼:
首先 我們來看主要功能代碼:
假設我現在想實現:在 datasearch.vue 中設置一個搜索按鈕,點擊搜索就跳轉至 datadisplay.vue 頁面,并且 datadisplay.vue 頁面會重新刷新渲染(不管路由是否變化)
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="請輸入內容" value-key="value" @change="sousuo" > <!-- 使用 value-key 屬性,可以指定任意列作為顯示用的 --> <!-- 自定義模板 --> <!-- 比如多個顯示 --> <!-- <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; // 調用 callback 返回建議列表的數據 cb(results); }, createFilter(queryString) { return (restaurant) => { // restaurant.value.toLowerCase().indexOf(queryString.toLowerCase())如果元素存在列表中返回下標,否則返回-1 console.log(restaurant.value.toLowerCase().indexOf(queryString.toLowerCase())); return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1); // 如果大于-1則代表有這個關鍵字 }; }, 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/> <!-- 這里必須要通過 :exclude 設置 緩存黑名單 否則跳轉會出問題 --> <!-- 黑名單中要包含 中轉的 vue 名 和中轉后的 vue 名 --> <keep-alive :exclude="exclude"> <!-- 通過key設置頁面刷新 規(guī)矩 $route.fullPath 得到路由(包含帶?的參數) :key="$route.fullPath" 如果路由改變就刷新 --> <!-- <router-view :key="$route.fullPath"></router-view>--> <!-- <router-view :key="$route.fullPath"></router-view>--> <!-- 這里設置或不設置 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 { // 用來中轉,避免路由不變時 頁面不刷新 name: "zhongzhuan", created() { this.pushUrl() }, methods: { getData(){ return this.$route.query.state }, pushUrl(){ this.$router.push({ path: '/datadisplay', query: {state: this.getData()}, // 傳遞參數,放在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 實際上就是全局路由對象任何頁面都可以調用 push(), go()等方法; // this.$route 表示當前正在用于跳轉的路由器對象,可以調用其name、path、query、params等屬性。 // 應此需要接受路由參數時,要用this.$route,發(fā)送跳轉路由時要用this.$router console.log(this.$route); this.content = this.$route.query; } } } </script> <style scoped> </style>
對應代碼實現圖片:
- 第一次點擊
- 第二次點擊
- 第三次點擊
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。