【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;
  }
相关推荐
candyTong1 天前
一觉醒来,大模型就帮我排查完页面性能问题
前端·javascript·架构
魔术师Grace1 天前
我给 AI 做了场入职培训
前端·程序员
玩嵌入式的菜鸡1 天前
网页访问单片机设备---基于mqtt
前端·javascript·css
前端一小卒1 天前
我用 Claude Code 的 Superpowers 技能链写了个服务,部署前差点把服务器搞炸
前端·javascript·后端
滑雪的企鹅.1 天前
HTML头部元信息避坑指南大纲
前端·html
一拳不是超人1 天前
老婆天天吵吵要买塔罗牌,我直接用 AI 2 小时写了个在线塔罗牌
前端·ai编程
阿丰资源1 天前
SpringBoot+Vue实战:打造企业级在线文档管理系统
vue.js·spring boot·后端
excel1 天前
如何解决 Nuxt DevTools 中关于 unstorage 包的报错
前端
Rust研习社1 天前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
C澒1 天前
AI 生码 - API2Code:接口智能匹配与 API 自动化生码全链路设计
前端·低代码·ai编程