经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
uni-app开发经验分享九: 组件传值
来源:cnblogs  作者:林恒  时间:2020/12/8 9:04:23  对本文有异议

一、父组件向子组件传值

通过props来实现,子组件通过props来接收父组件传过来的值!

1、逻辑梳理

父组件中:

第一步:引入子组件;

  1. import sonShow from '../../component/son.vue';

第二步:在components中对子组件进行注册;

  1. components: {
  2. sonShow
  3. },

第三步:以标签的形式载入;通过数据绑定的形式进行传值~

  1. <son-show :reciveUserInfo="userInfo"></son-show>

子组件中:

通过props接收父组件中传过来的值;

  1. props:["reciveUserInfo"],

2、代码展示

父组件index.vue

  1. <template>
  2. <view class="content">
  3. <son-show :reciveUserInfo="userInfo"></son-show>
  4. </view>
  5. </template>
  6. <script>
  7. import sonShow from '../../component/son.vue';
  8. export default {
  9. components: {
  10. sonShow
  11. },
  12. data() {
  13. return {
  14. userInfo: [{
  15. "userName": "kaliwo",
  16. "age": "19"
  17. },
  18. {
  19. "userName": "lihuahua",
  20. "age": "39"
  21. }
  22. ]
  23. }
  24. }
  25. }
  26. </script>

子组件son.vue

  1. <template>
  2. <view class="">
  3. <block v-for="(item,index) in reciveUserInfo" :key="index">
  4. <view class="mesg">
  5. <text>{{item.userName}}</text>
  6. <text>{{item.age}}</text>
  7. </view>
  8. </block>
  9. </view>
  10. </template>
  11. <script>
  12. export default{
  13. props:["reciveUserInfo"],
  14. }
  15. </script>
  16. <style>
  17. .mesg{
  18. display: flex;
  19. flex-direction: column;
  20. align-items: center;
  21. }
  22. </style>

3、结果

四、说明

对于一些详情页,比如有时我们需要点赞数量+1,-1的效果;但是,由于子组件不能改变父组件的值,所以直接操作从父组件接收的值进行更改是没有效果的!就像如下:

  1. let list = that.reciveUserInfo;
  2. for(var i in list){
  3. let tempAge = list[i].age + 1;
  4. list[i].age = tempAge;
  5. that.reciveUserInfo = list;
  6. }

年龄还是没有改变。所以应该怎么做了?

把从父组件接收到的值reciveUserInfo赋给一个新的变量mesgShow,对这个新的变量进行操作,然后用对齐进行渲染即可!

  1. let list = that.reciveUserInfo;
  2. for(var i in list){
  3. let tempAge = list[i].age + 1;
  4. list[i].age = tempAge;
  5. that.mesgShow = list;
  6. }

此时的结果为:age+1

 附加:改变的代码:

二、子组件向父组件传值

与微信小程序自定义组件中子组件向父组件传值一样的逻辑,都是通过事件,下面直接上代码:

父组件index.vue

  1. <template>
  2. <view class="content">
  3. <son-show @send="getSonValue"></son-show>
  4. </view>
  5. </template>
  6. <script>
  7. import sonShow from '../../component/son.vue';
  8. export default {
  9. components: {
  10. sonShow
  11. },
  12. methods:{
  13. getSonValue: function(res){
  14. console.log("res=========",res)
  15. }
  16. }
  17. }
  18. </script>

子组件;

  1. <template>
  2. <view class="" @click="sendMegToIndex">
  3. 点我向父组件传值
  4. </view>
  5. </template>
  6. <script>
  7. export default{
  8. methods:{
  9. sendMegToIndex: function(){
  10. // 向父组件传值
  11. this.$emit("send","我来自子组件")
  12. }
  13. }
  14. }
  15. </script>

最终结果:

 

原文链接:http://www.cnblogs.com/smileZAZ/p/14082222.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号