• vue 组件之间通信


    父传子

    **父组件代码**
    <template>
        <header-box :title-txt="showTitleTxt"></header-box>
    </template>
    <script>
        import Header from './header'
        export default {
            name: 'index',
            components: {
                'header-box': Header
            },
            data () {
                return {
                    showTitleTxt: '首页'
                }
            }
        }
    </script>

      

    **子组件代码**
    <template>
        <header>
            {{thisTitleTxt}}
        </header>
    </template>
    <script>
        export default {
            name: 'header-box',
            props: {
                titleTxt: String
            },
            data () {
                return {
                    thisTitleTxt: this.titleTxt
                }
            }
        }
    </script>
    

    子传父1

    **子组件代码**
    <template>
        <header>
           <button @click='anniu'></button>
        </header>
    </template>
    <script>
        export default {
            name: 'header-box',
            props: {
                titleTxt: String
            },
            data () {
                return {
                    
                }
            },
                       methods: {
                              anniu(){
                                     this.titleTxt('子组件传来的值')
                              },
                       },
        }
    </script>
    

      

    **父组件代码**
    <template>
        <header-box :title-txt="showTitleTxt"></header-box>
    </template>
    <script>
        import Header from './header'
        export default {
            name: 'index',
            components: {
                'header-box': Header
            },
            data () {
                return {
                    showTitleTxt: '首页'
                }
            },
            methods:{
                 showTitleTxt(a){
                    console.log(a) //子组件传来的值
                },
        
            }
        }
    </script>        
    

    子传父2

    **子组件代码**
    <template>
        <button @click="incrementCounter">{{counter}}</button>
    </template>
    <script>
        export default {
            name: 'button-counter',
            data () {
                return {
                    counter: 0
                }
            },
            metheds: {
                incrementCounter () {
                    this.$emit('increment','子组件传来的值')
                    this.counter++
                }
            }
        }
    </script>
    
    *通过$on,$emit*
    **父组件代码**
    <template>
        <div id="counter-event-example">
          <p>{{ total }}</p>
          <button-counter v-on:increment="incrementTotal"></button-counter>
    </div>
    </template>
    <script>
        import ButtonCounter from './buttonCounter'
        export default {
            name: 'index',
            components: {
                'button-conuter': ButtonCounter
            },
            data () {
                return {
                    total: 0
                }
            },
            methods: {
                incrementTotal (a) {
                    console.log(a) //子组件传来的值
                    this.total++
                }
            }
        }
    </script>          
    

      

  • 相关阅读:
    析构函数可以内联吗(可以,但不建议)
    auto_ptr
    Oracle Enterprise Linux 6.0配置本地yum
    标准C++输入输出和字符串类学习小程序集锦
    [转载]解决mysql“Access denied for user 'root'@'localhost'”
    [Linux网络编程学习笔记]套接字地址结构
    javascript基础
    java学习笔记14动态代理
    2013面试总结_01
    jquery实现复选框checkbox全选(完善)
  • 原文地址:https://www.cnblogs.com/ruthless/p/10411491.html
Copyright © 2020-2023  润新知