首页 > 其他 > 详细

Vue props 单向数据流

时间:2017-12-18 16:31:10      阅读:215      评论:0      收藏:0      [点我收藏+]

1、props通信

注意:DOM模板的驼峰命名props要转为短横分割命名。

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>Vue</title>
    </head>

    <body>
        <div id="app">
            <my-component message=‘来自父组件的数据‘ warning-text="提示信息"></my-component>

        </div>
        <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
        <script type="text/javascript">
            Vue.component(my-component, {
                template: <div>{{warningText}}:{{message}}</div>,
                props: [message, warningText]
            })
            new Vue({
                el: "#app"
            })
        </script>
    </body>

</html>

传递动态数据(v-bind):

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>Vue</title>
    </head>

    <body>
        <div id="app">
            <input type="text" v-model="parentMessage" />
            <my-component :message=‘parentMessage‘></my-component>

        </div>
        <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
        <script type="text/javascript">
            Vue.component(my-component, {
                template: <div>{{message}}</div>,
                props: [message]
            })
            new Vue({
                el: "#app",
                data: {
                    parentMessage: ‘‘
                }
            })
        </script>
    </body>

</html>

2、单向数据流

(1)子组件保存父组件传递过来的值,在子组件自己的作用域下修改和使用。

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>Vue</title>
    </head>

    <body>
        <div id="app">

            <my-component :init-count=‘1‘></my-component>

        </div>
        <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
        <script type="text/javascript">
            Vue.component(my-component, {
                template: <div>{{initCount}}</div>,
                props: [initCount],
                data: function() {
                    return this.initCount
                }
            })
            new Vue({
                el: "#app"
            })
        </script>
    </body>

</html>

(2)使用计算属性

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>Vue</title>
    </head>

    <body>
        <div id="app">

            <my-component :width=‘100‘></my-component>

        </div>
        <script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script>
        <script type="text/javascript">
            Vue.component(my-component, {
                template: <div :style="style">组件内容</div>,
                props: [width],
                computed: {
                    style: function() {
                        return {
                            width: this.width + px
                        }
                    }
                }
            })
            new Vue({
                el: "#app"
            })
        </script>
    </body>

</html>

 

Vue props 单向数据流

原文:http://www.cnblogs.com/mengfangui/p/8057613.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!