uniapp 子组件向父组件传值

ゝ一纸荒年。 2021-11-24 01:08 1337阅读 0赞

使用组件可以减少代码的重复率,提高写代码的效率,改起来也方便。

最近在使用uni-app做项目,一套代码多端实现,做些简单的项目还是可以的。废话少说,说说子组件向父组件传值。

子组件获取到值的时候,使用$emit传给父组件:

  1. this.$emit("getChild1",this.list);

父组件首先引入子组件:

  1. import child1 from './child1'

注册子组件:

  1. components: {
  2. child1,
  3. child2
  4. },

然后可以使用子组件:

  1. <child1 @getChild1 = "getChild1" v-if="current==0"></child1>

注意,子组件的提交的函数名,和父组件里使用子组件@后的名字是一样的。

20190727125116805.png

2019072712441717.png

但是要注意:子组件获取数据的函数要在created(){}里面获取,这个坑,要注意!!

在methods里可以获取到子组件的数据:

  1. getChild1(e){
  2. console.log(e)
  3. },

效果大概就是这样:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaWNob25nMTIz_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaWNob25nMTIz_size_16_color_FFFFFF_t_70 1

代码我也贴出来好了:

首页是,目录结构:watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaWNob25nMTIz_size_16_color_FFFFFF_t_70 2

res.json:

  1. {
  2. "code": 100,
  3. "fruitData": [
  4. "西红柿",
  5. "黄瓜",
  6. "苹果"
  7. ],
  8. "filmData": [
  9. "西红柿首富",
  10. "101次求婚",
  11. "肖申克的救赎"
  12. ]
  13. }

child1:

  1. <template>
  2. <view class="content">
  3. <view v-for="(item,index) in list" :key="index">{
  4. {item}}</view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. list: []
  12. }
  13. },
  14. components: {
  15. },
  16. onLoad() {},
  17. created() {
  18. var data = require('../../static/res.json')
  19. this.list = data.fruitData
  20. this.$emit("getChild1",this.list);
  21. },
  22. methods: {
  23. }
  24. }
  25. </script>
  26. <style>
  27. .content {
  28. display: flex;
  29. flex-direction: column;
  30. align-items: center;
  31. justify-content: center;
  32. }
  33. .logo {
  34. height: 200upx;
  35. width: 200upx;
  36. margin-top: 200upx;
  37. margin-left: auto;
  38. margin-right: auto;
  39. margin-bottom: 50upx;
  40. }
  41. .text-area {
  42. display: flex;
  43. justify-content: center;
  44. }
  45. .title {
  46. font-size: 36upx;
  47. color: #8f8f94;
  48. }
  49. </style>

child2:

  1. <template>
  2. <view class="content">
  3. <view v-for="(item,index) in list" :key="index">{
  4. {item}}</view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. list: []
  12. }
  13. },
  14. components: {
  15. },
  16. onLoad() {},
  17. created() {
  18. var data = require('../../static/res.json')
  19. this.list = data.filmData
  20. this.$emit("getChild2",this.list);
  21. },
  22. methods: {
  23. }
  24. }
  25. </script>
  26. <style>
  27. .content {
  28. display: flex;
  29. flex-direction: column;
  30. align-items: center;
  31. justify-content: center;
  32. }
  33. .logo {
  34. height: 200upx;
  35. width: 200upx;
  36. margin-top: 200upx;
  37. margin-left: auto;
  38. margin-right: auto;
  39. margin-bottom: 50upx;
  40. }
  41. .text-area {
  42. display: flex;
  43. justify-content: center;
  44. }
  45. .title {
  46. font-size: 36upx;
  47. color: #8f8f94;
  48. }
  49. </style>

parent:

  1. <template>
  2. <view class="content">
  3. <view class="btn-container">
  4. <button @click="getData(index)" v-for="(item,index) in list" :key="index">{
  5. {item}}</button>
  6. </view>
  7. <child1 @getChild1 = "getChild1" v-if="current==0"></child1>
  8. <child2 @getChild2 = "getChild2" v-if="current==1"></child2>
  9. </view>
  10. </template>
  11. <script>
  12. import child1 from './child1'
  13. import child2 from './child2'
  14. export default {
  15. data() {
  16. return {
  17. list:[
  18. '水果',
  19. '电影'
  20. ],
  21. current:0
  22. }
  23. },
  24. onLoad() {
  25. },
  26. components: {
  27. child1,
  28. child2
  29. },
  30. methods: {
  31. getData(index){
  32. this.current = index
  33. },
  34. getChild1(e){
  35. console.log(e)
  36. },
  37. getChild2(e){
  38. console.log(e)
  39. }
  40. }
  41. }
  42. </script>
  43. <style>
  44. .content {
  45. display: flex;
  46. flex-direction: column;
  47. align-items: center;
  48. justify-content: center;
  49. }
  50. .btn-container{
  51. display: flex;
  52. justify-content: space-between;
  53. align-items: center;
  54. }
  55. </style>

大概就是这些。

发表评论

表情:
评论列表 (有 0 条评论,1337人围观)

还没有评论,来说两句吧...

相关阅读