【Tip】父子组件传值和页面跳转传值方法(vue和uniapp)

本文介绍了在Vue和uniapp中子组件和父组件之间的传值方法、页面跳转传值方法。

一、父子组件传值

以列表为例:

1、子组件

sonTemplate.vue

(1)挂载显示内容

<el-table>

//列表内容

<el-table>

(2)关键方法

Creat()方法: 子组件列表getList()

Props:绑定要传递的值(父组件传给子组件==用于显示或在子组件上一次操作值基础上操作)

Props:{

sonValue: {
type: Number|String,
},

}

Submit():$emit("方法名method1", values) 传递在子组件操作的值 (子组件传给父组件==值传递)

2、父组件

(1) 挂载内容

javascript 复制代码
<son-template :sonValue="selectValue" @method1="getSelectValue">

(2)关键方法

javascript 复制代码
Import sonTemplate.vue

Compoent:{SonTemplate}

Data(){

selectValue: null,

}

Methods:{

getSelectValue(val){

this.selectValue = val

// 可对val做其他处理filter、map等}

}

二、路由自定义页面跳转和传值(在Vue中)

1、页面跳转

javascript 复制代码
this.$store.dispatch('tagsView/delView', this.$route)

this.$router.push({
  path: '/factory/factoryCheckPlanManager/add',   //'/factory/factoryCheckPlanManager/add为跳转文件的vue目录
  query: {checkPlanId: id, queryParams: this.queryParams}
})

2、接收

javascript 复制代码
create(){

let queryParams = this.$route.query.queryParams

}

三、路由自定义页面跳转和传值(在uniapp中)

1、页面跳转之===父子组件传值(子组件传递给父组件)

页面之间子页面(下一页面)可用$emit返回给父级页面(上一页面),父级页面在onShow方法中监听事件,实现页面之间的组件传值。

常用于较大数据的传送,不宜放在请求地址里

(1) 子页面传值

javascript 复制代码
uni.$emit('updateCheckItem', {
  type:'updateCheckItem',
  checkItem: this.checkItem,
  locationId:Number(this.checkItem.checkUnitLocation)
});

//返回上级页面

uni.navigateBack({
  delta: 1
})

(2) 父页面(上一页面)接收

javascript 复制代码
onShow(){

uni.$off('updateCheckItem');
uni.$once('updateCheckItem', data => {
  if(data.type === "updateCheckItem") {
    that.checkList.forEach((check,key)=>{
      if(check.pointUnitId===data.locationId){
        check.checkItemList.forEach((item,index)=>{
          if(item.id===data.checkItem.id){
            that.checkList[key].checkItemList[index]=data.checkItem;
          }
        })
      }
    })
    uni.hideLoading();
  } else {
    console.log('失败')
  }
})

}

2、页面跳转之===地址跳转

父页面跳转到子页面,要传递的参数值附带在请求地址上,子页面在onLoad函数中接收

(1) 父页面跳转传值

javascript 复制代码
let url="sonVue?describe="+this.checkItem.abnormalDescribe+'&locationId='+this.checkItem.checkUnitLocation;
this.$tab.navigateTo(url);

(2) 子页面接收

sonVue.vue

javascript 复制代码
onLoad(option){
  if(option.describe!=null && option.describe!='null'){
    this.describe=option.describe;
  }
相关推荐
玲小珑几秒前
请求 ID 跟踪模式:解决异步请求竞态条件
前端
开心_开心急了8 分钟前
AI+PySide6实现自定义窗口标题栏目(titleBar)
前端
开心_开心急了14 分钟前
Ai加Flutter实现自定义标题栏(appBar)
前端·flutter
布列瑟农的星空18 分钟前
SSE与流式传输(Streamable HTTP)
前端·后端
GISer_Jing29 分钟前
跨境营销前端AI应用业务领域
前端·人工智能·aigc
oak隔壁找我36 分钟前
Node.js的package.json
前端·javascript
talenteddriver40 分钟前
web: http请求(自用总结)
前端·网络协议·http
全栈派森43 分钟前
Flutter 实战:基于 GetX + Obx 的企业级架构设计指南
前端·flutter
Awu12271 小时前
Vue3自定义渲染器:原理剖析与实践指南
前端·vue.js·three.js
支撑前端荣耀1 小时前
从零实现前端监控告警系统:SMTP + Node.js + 个人邮箱 完整免费方案
前端·javascript·面试