Vue页面间传值,【params】和【query】以及localStorage缓存传值(obj缓存传值)

本文探讨了在Vue.js应用中如何通过`params`和`query`进行页面间参数传递,并展示了在不同页面如何获取这些值。同时,也介绍了如何使用本地缓存(localStorage)来存储和检索数据,确保数据在页面跳转后仍然可用。
摘要由CSDN通过智能技术生成

一,【params】和【query】传值:

传值页面:
<template>
   <div>
     <el-card class="post-card" v-for="item in postList" v-bind:key="item.id" 
           v-on:click="turnToPost(item.id)">            
     </el-card>
   </div>
 </template>

 <script>
 export default {
   data() {
     return {
         postList: [{ id:1 }]
     };
   },
   methods: {
     turnToPost: function(id) {
       //参数传值
       this.$router.push({
         name: "about",//页面名称
         //path: '/blog/about',//name和path用其一就可以
         params: { id: id, postList:JSON.stringify(this.postList) },//post,params放法
         query: { id: id }//post,query放法
       });
     }
   }
 };
 </script>

取值页面

<template>
  <div class="about">
     <h1>about</h1>
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  mounted: function() {
      let post = this.$route.params.id; //params
      let posts = JSON.parse(this.$route.params.postList);//params
      let get1 = this.$route.query.id; //query
  },
  methods: {}
};
</script>

二,用本地缓存传值:

传值页面:
<template>

    <div> <el-card class="post-card"  @click="shenhe"> </el-card> </div>

</template>

<script>

export default {

  data() {

     return {

         objdata:{ name:'你好',id:1 },

         time:'2021-10-02'

         }

     },

methods: { 

 shenhe(){
    localStorage.setItem('objdata',JSON.stringify(objdata))//参数传值obj
    localStorage.setItem('time',time)//参数传值 单个   
 }
}
</script>

取值页面:

<template>
  <div class="about">
    <h1>about</h1>
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  mounted: function() {
      let time = localStorage.getItem("time"); //获取值单个
      let objdata = JSON.parse(localStorage.getItem('objdata'))//获取obj
  },
  methods: {}
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3的不同页面传递数据通常涉及使用 Vue Router 进行路由管理,并利用 Vuex 或者 Composition API 的上下文提供全局状态管理。下面是这两种方法的具体说明: ### 使用 Vuex **原理及步骤**: 1. **安装 Vuex**: 首先需要安装 Vuex,通过 `npm install vuex` 或 `yarn add vuex` 来安装。 2. **创建 Vuex Store**: 创建 Vuex store 文件(例如 `store/index.js`),并在其中定义 state、mutations、actions 和 getters。 ```javascript import { createStore } from 'vuex'; export default createStore({ state: { sharedData: '', }, mutations: { setSharedData(state, payload) { state.sharedData = payload; }, }, actions: { updateSharedData({ commit }, payload) { commit('setSharedData', payload); }, }, getters: { getSharedData: (state) => state.sharedData, }, }); ``` 3. **配置路由并挂载 Vuex**: 在 `router/index.js` 中引入 Vuex 实例并将其作为全局服务使用。 ```javascript import Vue from 'vue'; import Vuex from 'vuex'; import router from './router'; // 其他导入... Vue.use(Vuex); const store = new Vuex.Store({ // Vuex store 定义... }); new Vue({ router, store, render: (h) => h(App), }).$mount('#app'); ``` 4. **在组件传递数据**: - 在接收数据的组件中,可以监听 Vuex store 的变化,或者直接访问 store 中的数据。 - 在更新数据的组件中,使用 `this.$store.dispatch` 调用 action 来触发 Vuex 中的操作,然后由 action 触发 mutation 来修改 state。 5. **使用 computed 或者 methods 访问数据**: 可以在组件内使用 Vuex store 的 getters 获取共享数据。 ### 使用 Composition API **原理及步骤**: 1. **引入 Composables**: 引入 Vuex 依赖项 `useStore` 以及 `defineStore` 函数来简化状态管理。 2. **创建 Composable**: 使用 `createStore` 函数定义 store,然后在组件中使用 `useStore` 提供的 Composable 函数来获取或设置共享数据。 ```javascript import { defineStore } from "pinia"; const useGlobalState = defineStore("globalState", { state: () => ({ sharedData: "", }), getters: { getData: (state) => state.sharedData, }, actions: { setData(data) { this.state.sharedData = data; }, }, }); ``` 3. **在组件中使用 Composable**: 在需要使用共享数据的组件中,使用 `useStore()` 函数引用 store,并通过 `mapGetters` 或 `mapActions` 来映射 getters 和 actions 到组件的计算属性或方法。 ```html <template> <!-- 使用 computed 属性 --> {{ globalState.getData }} <!-- 使用方法 --> <button @click="updateData">Update Data</button> </template> <script setup lang="ts"> import { useGlobalState } from "../stores/globalState"; import { onMounted } from "vue"; const globalState = useGlobalState(); const updateData = () => { globalState.setData("new value"); }; onMounted(() => { console.log(globalState.getData); // 初始化时调用 getters 获取数据 }); </script> ``` 以上就是 Vue3 不同页面传递数据的基本方法,无论是通过 Vuex 还是使用 Composition API,都可以有效地实现在应用中的全局状态管理和数据通信。在选择使用哪种方法时,可以根据项目的大小、复杂度和个人偏好来决定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值