vue2.0 靜態prop和動態prop


動態prop:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="../vue2.2.js"></script>
    </head>
    <body>
        <div id="app">
            <div>
                <input v-model='parentMsg' />
                <br />
                <child v-bind:my-Message="parentMsg"></child>
                <!--可以理解成var mymesage = parentMsg;-->
            </div>
        </div>
        <template id="simpleDemo">
            <div> {{myMessage}} </div>
        </template>
        <script>
            /*props可以是數組或者是對象,用於接收父組件的數據. props 對象允許配置高級選項,HTML特性不區分大小寫,名字形式為駝峰式的prop作為特性時,我們 需要轉為 a-b (短橫線隔開)*/
            var vm = new Vue({ el: "#app", data: { parentMsg: "hello Prop" }, components: { 'child': { template: "#simpleDemo", props: ["myMessage"], //自定義名字
                        /*props:{ type:String, required:true }, props:{ type:Number, default:100 }*/ } } }) </script>
    </body>

</html>

靜態prop:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="../vue2.2.js"></script>
        <link rel="stylesheet" href="styles/demo.css" />
    </head>
    <body>
        <div id="app">
            <my-component :my-name="name" :my-age="age"></my-component>
        </div>
        <template id="myComponent">
            <table>
                <tr>
                    <th colspan="2">子組件</th>
                </tr>
                <tr>
                    <td>名字</td>
                    <td>年齡</td>
                </tr>
                <tr>
                    <td>{{myName}}</td>
                    <td>{{myAge}}</td>
                </tr>
            </table>
        </template>
        <script>
            var vm = new Vue({ el: "#app", data: { name: "小明", age: 24 }, components: { 'my-component': { template: "#myComponent", props: ["myName", "myAge"] } } }) </script>
    </body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM