vue實戰2-----怎麼將數據帶到另一個頁面並反顯,並返回到原頁面,數據還在?

怎麼將數據帶到另一個頁面並反顯,並返回到原頁面,數據還在?
一,先結局第一個問題,怎麼將數據帶到另一個頁面並反顯出來
1,第一個方法:將參數放在地址後面,也就是get請求:地址?+參數(也就是傳過去參數)

onDetail() {
        console.log(this.listQuery.departName)
        this.$router.push("/couponsSystem/couponsManage/fullCutCoupons/index?channel="+this.listQuery.channel+"&scene="+this.listQuery.scene+"&departName="+this.listQuery.departName+"&couponFirstType="+this.listQuery.couponFirstType);
      },

2,怎麼接收參數?

created() {
    this.getParams()
    }
methods:{
getParams() {
      this.listQuery.channel = **this.$route.query.channel**//注意是這樣get到傳過來的參數的
      this.listQuery.couponFirstType = this.$route.query.couponFirstTypeId
      this.listQuery.scene = this.$route.query.sceneId
      this.listQuery.departName = this.$route.query.departName
      this.listQuery.couponFirstTypeName= this.$route.query.couponFirstTypeName
      this.listQuery.sceneName= this.$route.query.sceneName
      this.listQuery.departId = this.$route.query.departId
    },

}

二:第二種方法:參數放在地址後面肯定不安全,所以改成放在cookie裏面
首先:創建一個工具類–創建存放進cookie和從cookie中取出的方法

**import Cookies from 'js-cookie'**//注意這句話--要先引入cookie

export function getListQuery(key) {

  if (Cookies.get(key) != undefined && Cookies.get(key) != "") {
    return JSON.parse(Cookies.get(key))
  }
  return ""
}

export function setListQuery(key,value) {
  var listQuery = JSON.stringify(value)
  Cookies.set(key, listQuery)
}

export function removeListQuery(key) {
  return Cookies.remove(key)
}


export function getNavigation() {
  return Cookies.get("navigation")
}

export function setNavigation(navigation) {
  return Cookies.set("navigation", navigation)
}

export function setCreation(key,value) {
  var creation = JSON.stringify(value)
  Cookies.set(key, creation)
}

export function getCreation(key) {

  if (Cookies.get(key) != undefined && Cookies.get(key) != "") {
    return JSON.parse(Cookies.get(key))
  }
  return ""
}

export function setSearch(key,value) {
  var search = JSON.stringify(value)
  Cookies.set(key, search)
}

export function getSearch(key) {

  if (Cookies.get(key) != undefined && Cookies.get(key) != "") {
    return JSON.parse(Cookies.get(key))
  }
  return ""
}

export function getFlag() {
  return Cookies.get("flag")
}

export function setFlag(flag) {
  return Cookies.set("flag", flag)
}

export function getSaveFlag() {
  return Cookies.get("saveFlag")
}

export function setSaveFlag(saveFlag) {
  return Cookies.set("saveFlag", saveFlag)
}

第二步:set數據到cookie

import {setCreation} from '@/utils/costStatsCookie'
onDeatail(){
var creation = {channel:""+this.listQuery.channel+"", sceneName:""+dataName+"", sceneId:""+dataId+"", departName:""+dName+"", departId:""+dId+"", couponFirstTypeName:""+cftName+"", couponFirstTypeId:""+cftId+""};
        setCreation("creation",creation)
        this.$router.push("/couponsSystem/couponsManage/"+url+"/index");
}

第三步:從cookie中get數據

import {getCreation} from '@/utils/costStatsCookie'
getParams() {
      var creationParams = getCreation("creation")
      if(creationParams!= "") {
        this.listQuery.channel = creationParams.channel
        this.listQuery.couponFirstType = creationParams.couponFirstTypeId
        this.listQuery.scene = creationParams.sceneId
        this.listQuery.departName = creationParams.departName
        this.listQuery.couponFirstTypeName = creationParams.couponFirstTypeName
        this.listQuery.sceneName = creationParams.sceneName
        this.listQuery.departId = creationParams.departId
      }
    },

二,第二個問題:怎麼使跳轉到另一個頁面,傳過去的數據,再返回到原頁面的時候數據還在?

解決方法(思路):跳轉到另外一個頁面並帶數據之前,將數據放在cookie中,等返回到原頁面的時候,再從cookie中取出來就可以了。

實現代碼:
跳轉前的頁面的代碼有如下:

 import {setSearch,getSearch,getFlag,setFlag} from '@/utils/costStatsCookie'
methods(){
	onDetail(){
	**setSearch("search",this.listQuery)**
	        var creation = {channel:""+this.listQuery.channel+"", sceneName:""+dataName+"", sceneId:""+dataId+"", departName:""+dName+"", departId:""+dId+"", couponFirstTypeName:""+cftName+"", couponFirstTypeId:""+cftId+""};
	        setCreation("creation",creation)
	        this.$router.push("/couponsSystem/couponsManage/"+url+"/index");
	      },
	}
}
created(){
 //初始化頁面時獲取cookie中保留的搜索記錄
      var flag = getFlag()
      if(getSearch("search") != "" && flag == "false"){
        this.listQuery = getSearch("search")
         flag = "true";
         setFlag(flag)
        this.getList();
      }
}

跳轉之後的那個頁面的代碼:

import {getCreation,setFlag,setSaveFlag} from '@/utils/costStatsCookie'
 back() {
      var flag = "false";
      setFlag(flag)
      this.$router.push("/couponsSystem/couponsManage/couponsList/index");
    },

注意:setFlag和getFlag中的flag的作用是標識作用,也就是啥時候返回到原頁面,啥時候原頁面纔去取跳轉之前放到cookie中的數據,要不然如果沒有這個標識,直接取,就會產生:只要跳轉到這個頁面就會取出來這些數據顯示到頁面中,如果沒有關閉瀏覽器,比如用戶退出換了個賬戶登錄,一進這個頁面,這些數據取出來還會顯示到這個頁面,肯定不對嘛。因爲用戶根本沒有操作任何,怎麼就會有數據顯示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章