码迷,mamicode.com
首页 > 其他好文 > 详细

使用组件要注意的细节点

时间:2019-07-11 22:02:25      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:数据存储   char   root   static   问题   code   动画   comm   world   

<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="./vue.js"></script> <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> --> </head> <body> <div id="root"> <table> <tbody> <!-- <tr><td>1</td></tr> <tr><td>2</td></tr> <tr><td>3</td></tr> --> <!-- //虽然会显示出来,但有问题 <br> --> <row></row> <row></row> <row></row> <!-- //可行 <br> --> <tr is="row"></tr> <tr is="row"></tr> <tr is="row"></tr> <!-- //可行 <br> --> <ul> <li is="list"></li> <li is="list"></li> <li is="list"></li> </ul> </tbody> </table> </div> <script type="text/javascript"> Vue.component("row", { template: "<tr><td>1</td></tr>" }); Vue.component("list", { template: "<li>1</li>" }); var vm = new Vue({ el: "#root" }); </script> </body> </html>

在子组件里定义data必须是函数,不能是对象(为了让子组件之间不共享数据,通过函数返回让每个子组件都有独立的数据存储):

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
    <table>
        <tbody>

            <tr is="row"></tr>
            <tr is="row"></tr>
            <tr is="row"></tr>

        </tbody>
    </table>
</div>
<script type="text/javascript">
    Vue.component("row", {

        /*//然而,在子组件,而不是根组件new Vue()中通过对象定义data是不可以的,而必须是函数
        data: {
            content: "this is a row"
        },*/

        //这才是正确的:
        data: function() {
            return {
                content: "this is content"
            }
        },
        template: "<tr><td>{{content}}</td></tr>"
    });
    Vue.component("list", {
        template: "<li>1</li>"
    });
    var vm = new Vue({
        el: "#root"
    });
</script>
</body>
</html>

vue不建议操作dom,但在处理一些复杂的动画效果,光靠vue的数据绑定并不一定能满足情况。vue中通过ref操作dom:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
    <div ref="hello" @click="handleClick">hello world</div>
</div>
<script type="text/javascript">
    var vm = new Vue({
        el: "#root",
        methods: {
            handleClick: function() {
                //获取指定dom节点
                console.log(this.$refs.hello.innerHTML)
            }
        }
    });
</script>
</body>
</html>

如果是获取组件的dom呢?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
    <counter ref="one" @change="handleChange"></counter>
    <counter ref="two" @change="handleChange"></counter>
    <div>{{total}}</div>
</div>
<script type="text/javascript">
    Vue.component("counter", {
        template: "<div @click=‘handleClick‘>{{number}}</div>",
        data: function() {
            return {
                number: 0
            }
        },
        methods: {
            handleClick: function() {
                this.number++
                this.$emit("change")
            }
        }
    })
    var vm = new Vue({
        el: "#root",
        data: {
            total: 0
        },
        methods: {
            handleChange: function() {
                /*console.log(this.$refs.one.number)
                console.log(this.$refs.two.number)*/
                this.total = this.$refs.one.number + this.$refs.two.number
            }
        }
    });
</script>
</body>
</html>

技术图片

使用组件要注意的细节点

标签:数据存储   char   root   static   问题   code   动画   comm   world   

原文地址:https://blog.51cto.com/5660061/2419334

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!