原创

vue子组件向父组件传值(父子组件传值)


一直都是坐着杂七杂八的混合复杂开发,前端一直都是一知半解的状态。这次趁着项目经验,趁热记录一下。相信后面自己会用到,大家也有可能用到。

父组件代码:

  1. //parentFn 在父页面中处理的函数 childFn在子页面【Huati页面】需要处理的函数
  2. <Huati @childFn="parentFn"></Huati>
  3. <script>
  4. import Huati from "@/components/opt/article/huati"
  5. export default {
  6. name: 'voi',
  7. data() {
  8. return {
  9. form: {
  10. description:''
  11. }
  12. };
  13. },
  14. methods: {
  15. parentFn(payload) {
  16. //负组件获取子组件的值
  17. console.log(payload);
  18. this.form.description = payload
  19. },
  20. },
  21. created() {
  22. },
  23. mounted() {
  24. },
  25. components: {
  26. UpLoadFile,
  27. Huati
  28. }
  29. }
  30. </script>

子页面

  1. <template>
  2. <div class="testCom">
  3. <input type="text" v-model="message" />
  4. <button @click="click">Send</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. // ...
  10. data() {
  11. return {
  12. // 默认
  13. message: '我是来自子组件的消息'
  14. }
  15. },
  16. methods: {
  17. click() {
  18. this.$emit('childFn', this.message); //通过childFn 将值传递给父页面
  19. }
  20. }
  21. }
  22. </script>
留言反馈
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈