zoukankan      html  css  js  c++  java
  • Vue.js中ref ($refs)用法举例总结

    原文:http://www.jianshu.com/p/3bd8a2b07d57

    看Vue.js文档中的ref部分,自己总结了下ref的使用方法以便后面查阅。

    一、ref使用在外面的组件上

    HTML 部分

    <div id="ref-outside-component" v-on:click="consoleRef">
        <component-father ref="outsideComponentRef">
        </component-father>
        <p>ref在外面的组件上</p>
    </div>

    js部分

        var refoutsidecomponentTem={
            template:"<div class='childComp'><h5>我是子组件</h5></div>"
        };
        var  refoutsidecomponent=new Vue({
            el:"#ref-outside-component",
            components:{
                "component-father":refoutsidecomponentTem
            },
            methods:{
                consoleRef:function () {
                    console.log(this); // #ref-outside-component     vue实例
                    console.log(this.$refs.outsideComponentRef);  // div.childComp vue实例
                }
            }
        });

    二、ref使用在外面的元素上

    HTML部分

    <!--ref在外面的元素上-->
    <div id="ref-outside-dom" v-on:click="consoleRef" >
        <component-father>
        </component-father>
        <p  ref="outsideDomRef">ref在外面的元素上</p>
    </div>

    JS部分

       var refoutsidedomTem={
            template:"<div class='childComp'><h5>我是子组件</h5></div>"
        };
        var  refoutsidedom=new Vue({
            el:"#ref-outside-dom",
            components:{
                "component-father":refoutsidedomTem
            },
            methods:{
                consoleRef:function () {
                    console.log(this); // #ref-outside-dom    vue实例
                    console.log(this.$refs.outsideDomRef);  //   <p> ref在外面的元素上</p>
                }
            }
        });

    三、ref使用在里面的元素上---局部注册组件

    HTML部分

    <!--ref在里面的元素上-->
    <div id="ref-inside-dom">
        <component-father>
        </component-father>
        <p>ref在里面的元素上</p>
    </div>

    JS部分

        var refinsidedomTem={
            template:"<div class='childComp' v-on:click='consoleRef'>" +
                           "<h5 ref='insideDomRef'>我是子组件</h5>" +
                      "</div>",
            methods:{
                consoleRef:function () {
                    console.log(this);  // div.childComp   vue实例 
                    console.log(this.$refs.insideDomRef);  // <h5 >我是子组件</h5>
                }
            }
        };
        var  refinsidedom=new Vue({
            el:"#ref-inside-dom",
            components:{
                "component-father":refinsidedomTem
            }
        });

    四、ref使用在里面的元素上---全局注册组件

    HTML部分

    <!--ref在里面的元素上--全局注册-->
    <div id="ref-inside-dom-all">
        <ref-inside-dom-quanjv></ref-inside-dom-quanjv>
    </div>

    JS部分

        Vue.component("ref-inside-dom-quanjv",{
            template:"<div class='insideFather'> " +
                        "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" +
                        "  <p>ref在里面的元素上--全局注册 </p> " +
                      "</div>",
            methods:{
                showinsideDomRef:function () {
                    console.log(this); //这里的this其实还是div.insideFather
                    console.log(this.$refs.insideDomRefAll); // <input  type="text">
                }
            }
        });
    
        var refinsidedomall=new Vue({
            el:"#ref-inside-dom-all"
        });


    作者:该帐号已被查封
    链接:http://www.jianshu.com/p/3bd8a2b07d57
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    python运行出现TypeError: super() takes at least 1 argument (0 given)错误
    python使用Pyinstaller打包
    python 将字符串中的unicode字符码转换成字符
    python 复制列表
    AetherUpload大文件传输
    phpstom激活
    BusyBox telnetd配置
    MDK链接脚本错误
    利用mass storage class 做免驱动usb设备.
    BMP图片的解析,关于压缩方式
  • 原文地址:https://www.cnblogs.com/zzcit/p/7744169.html
Copyright © 2011-2022 走看看