Vue 多个平行页面间传值,非组件间传递,简单易懂

一、使用路由传递参数


A页面和B页面是两个平行页面,非父子组件关系,先要将A页面的参数传递到B页面中。

⚠️注意 :query 中的参数 params ,需要使用  JSON.stringify({}) 方法,把对象转化成JSON字符串

A.vue 页面

<el-button type="primary" size="medium" icon="el-icon-plus" @click="add">新建订单</el-button>
add() {
    this.$router.push({
        path: '/ad/order/add' ,
        query: {params: JSON.stringify({id: 1, name: 'xiaoming'})}
    })
},

 

 

B.vue 页面

我们直接使用  this.$route.query.row 来获取路由传递过来的参数!

created() {
    this.params =  JSON.parse(this.$route.query.params) // 获取从router中传过来的参数
    console.log('数据是:' , this.params)
},

 

 

二、使用浏览器本地存储


A页面使用浏览器的本地存储方法,把要传递的参数,存储在浏览器中

A.vue 页面

// 本地存储
localStorage.setItem('params',JSON.stringify({id: 1, name: 'xiaoming'}))  

// 会话存储
sessionStorage.setItem('params',JSON.stringify({id: 1, name: 'xiaoming'})) 

B.vue 页面

// 本地存储中取值
let params = JSON.parse(localStorage.getItem('params'));  

// 会话存储中取值
let params = JSON.parse(sessionStorage.getItem('params'));  

 

1. sessionStorage

仅在当前会话下有效,关闭页面或者浏览器后被清除;存放数据大小一般为5MB;

// 存储:以“key”为名称存储一个值“value”
sessionStorage.setItem("key","value");

///获取名称为“key”的值
sessionStorage.getItem("key");

//删除名称为“key”的信息。
sessionStorage.removeItem("key");

​//清空sessionStorage中所有信息
sessionStorage.clear();

 

2.localStorage

生命周期永久,除非用户清除浏览器中的localStorage信息,否则永远存在;存放数据大小一般为5MB;仅在浏览器中保存,不参与服务器通信;

//以“key”为名称存储一个值“value”
localStorage.setItem("key","value");

//获取名称为“key”的值
localStorage.getItem("key");

//删除名称为“key”的信息。
localStorage.removeItem("key");

​//清空localStorage中所有信息
localStorage.clear();

 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Vue3 的 `<component>` 组件来动态切换两个组件,并通过 props 组件。 首先,你需要在页面中定义两个组件: ```typescript // ComponentA.vue <template> <div> <p>Component A</p> <p>{{ message }}</p> </div> </template> <script lang="ts"> import { defineComponent, PropType } from 'vue'; export default defineComponent({ props: { message: { type: String as PropType<string>, required: true, }, }, }); </script> ``` ```typescript // ComponentB.vue <template> <div> <p>Component B</p> <p>{{ count }}</p> </div> </template> <script lang="ts"> import { defineComponent, PropType } from 'vue'; export default defineComponent({ props: { count: { type: Number as PropType<number>, required: true, }, }, }); </script> ``` 然后,你可以在页面中使用 `<component>` 组件来动态切换这两个组件,并通过 props : ```typescript <!-- App.vue --> <template> <div> <component :is="currentComponent" :message="message" :count="count" /> <button @click="switchComponent">Switch Component</button> </div> </template> <script lang="ts"> import { defineComponent, reactive } from 'vue'; import ComponentA from './ComponentA.vue'; import ComponentB from './ComponentB.vue'; export default defineComponent({ components: { ComponentA, ComponentB, }, setup() { const state = reactive({ currentComponent: 'ComponentA', message: 'Hello, World!', count: 0, }); const switchComponent = () => { state.currentComponent = state.currentComponent === 'ComponentA' ? 'ComponentB' : 'ComponentA'; state.count++; }; return { ...state, switchComponent, }; }, }); </script> ``` 这里使用了 Vue3 的 Composition API 来定义页面组件,并使用 `reactive` 来定义一个响应式对象来存储组件状态。`switchComponent` 方法用于切换两个组件,并且每次切换都会增加 `count` 的。在模板中,通过 `:is` 来动态切换组件,同时通过 `:message` 和 `:count` 来传递组件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值