zoukankan      html  css  js  c++  java
  • 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>          
    

      

  • 相关阅读:
    XP下在控制面板和登录界面中隐藏用户方法
    DataGirdView 单元格限制内容输入参考(按键时的判断)
    VB.Net操作Excel
    VS扩展:标签左置 — productivity power tools
    使用VBS自动删除已经从FTP下载下来的文件
    VB.Net下ComboBox操作收集
    修改网卡IP信息的批处理文件
    VB.NET自定义控件 —— 添加控件自定义属性
    使用关键字对数组进行模糊查找;对一维字符串数组进行排序
    mysql多字段模糊查询
  • 原文地址:https://www.cnblogs.com/ruthless/p/10411491.html
Copyright © 2011-2022 走看看