zoukankan      html  css  js  c++  java
  • Vue父子组件传值$parent , ref,$refs,props大总结

    子组件:

    <template>
        <div class="child">
            <slot name='meiyong'></slot>
            <p >我是子组件哟 {{num}} {{ttttt}} {{nike}} 这是我独有的----->{{isChi}} </p>
            <slot name="strong"></slot>
        </div>
    </template>
    <script>
    import Utils from '@/utils/utils'
    export default {
        props:['ttttt','nike'],
        data(){
            return {
                num:" ",
                isChi:'★子组件独有★'
            }
        },
        mounted(){
            //传值给父组件
            this.$parent.runTime= Utils.formatDate(new Date(), "YYYY-MM-DD hh:mm:ss");
        }
        
    }
    </script>
    <style lang="scss" scoped>
        .child{
            height: 5rem;
             100%;
            background-color: skyblue;
            text-align: center;
            position: relative;
            p{
                height: 30px;
                 800px;
                display: block;
                position: absolute;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                margin: auto;
                // transform: translate(-50%,-50%)
            } 
            .parent{
            color:greenyellow;
            }
            .strong{
                position: absolute;
                bottom: 0;
                left: 50%;
                transform: translateX(-50%);
                color: darkmagenta;
            }
        }
    </style>

    父组件:

    <template>
        <div >
        <!-- 父传子-->
            <tc :ttttt='mongodb'  :nike='nike' ref="toChild">
                <template v-slot:meiyong >
                     <h1 class="parent">v-slot中的内容    没用?呵呵</h1>
                </template>
                <template v-slot:strong>
                    <h2 class="strong">你很强???{{runTime}} </h2>
                </template>
            </tc>
        </div>
    </template>
    <script>
    import testChild from '@/views/testChild'
    export default {
        data(){
            return {
                ttttt:'█我是父组件给child传的值█',
                mongodb:'█我是MongoDB哟█',
                nike:'███ just do ███',
                runTime:'null'
            }
        },
        mounted(){
            let chi = this.$refs.toChild
            console.log('chi===>',chi.isChi);  
            this.$refs.toChild.isChi = '我试试能不能修改子组件的值'
           
            // num不是传的值 所以要ref才能传
            this.$refs.ttttt.num = '1949'
        },
        components:{
            tc:testChild
        }
    }
    </script>
    <style lang="scss" scoped>
       
    </style>
  • 相关阅读:
    这一次,彻底弄懂 JavaScript 执行机制
    flex布局实例demo全解
    Sass和less的区别是什么?用哪个好
    开放服务器特定端口
    使用jacob报错:com.jacob.com.ComFailException: Can't get object clsid from progid
    计算机网络基础
    前端分页3
    window 10 Microsoft Edge 无法使用开发者模式【F12】
    docker下载安装
    git常用笔记
  • 原文地址:https://www.cnblogs.com/it-Ren/p/11335040.html
Copyright © 2011-2022 走看看