【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;
  }
相关推荐
mCell2 小时前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip2 小时前
Node.js 子进程:child_process
前端·javascript
excel5 小时前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel6 小时前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼7 小时前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping7 小时前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙8 小时前
[译] Composition in CSS
前端·css
白水清风8 小时前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix9 小时前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278009 小时前
new、原型和原型链浅析
前端·javascript