Vue構(gòu)造器里的選項(xiàng)

1.propsData選項(xiàng)

<body>
    <header></header>

    <script type="text/javascript">

        var header_a = Vue.extend({
            template:`<p>{{message}}-{{a}}</p>`,
            data:function(){
                return{
                    message:'Hello,I am header.'
                }
            },
            props:['a'],
        });

        //此處是標(biāo)簽 類名#header id則是.header
        new header_a({propsData:{a:111}}).$mount('header');
    </script>
</body>

2.computed選項(xiàng)

<body>
    <div id="app">
        <p>{{newPrice}}</p>
        <ul>
            <li v-for="news in reverNews">{{news.title}}-{{news.date}}</li>
        </ul>
    </div>

    <script type="text/javascript">

        var newsLists = [
            {title:'戰(zhàn)“疫”中 習(xí)近平始終牽掛困難群眾',date:'2017/3/24'},
            {title:'聲音·記“疫”  致敬這場(chǎng)戰(zhàn)疫的中堅(jiān)力量',date:'2017/3/25'},
            {title:'清明追思 家國(guó)永念丨那些應(yīng)被銘記的戰(zhàn)“疫”瞬間',date:'2017/3/26'},
            {title:'前行是最好的紀(jì)念',date:'2017/3/27'},
        ];
        var app = new Vue({
            el:'#app',
            data:{
                price:100,
                newsList:newsLists,
            },
            computed:{
                newPrice:function(){
                    return this.price = '¥' + this.price + '元';
                },
                reverNews:function(){
                    return this.newsList.reverse();//倒排序
                }
            },
        })
    </script>
</body>

3.methods選項(xiàng)

<body>
    <div id="app">
        <p>{{count}}</p>
        <button @click="add(3,$event)">add</button>

        <!-- 構(gòu)造器中調(diào)用methods方法 -->
        <p><btn @click.native="add(5)"></btn></p>
    </div>

    <!-- 外部調(diào)用methods方法 -->
    <button onclick="app.add(10)">外部ADD</button>

    <script type="text/javascript">
        var btn = {
            template:`<button>組件ADD</button>`
        }

        var app = new Vue({
            el:'#app',
            data:{
                count:1,
            },

            components:{
                "btn":btn,
            },

            methods:{
                add:function(num,event){
                    if(num != ''){
                        this.count += num;
                    }else{
                        this.count ++;
                    }

                    console.log(event);//event包括點(diǎn)擊操作的詳細(xì)信息列表
                }
            }
        })
    </script>
</body>

4.watch選項(xiàng)

<body>
    <div id="app">
        <p>今日溫度:{{temperature}} 度</p>
        <p>穿衣建議:{{cloth}}</p>
        <p><button @click="add()">升溫</button><button @click="less()">降溫</button></p>
    </div>

    <script type="text/javascript">
        var clothsArr = [
            "T恤短袖","夾克長(zhǎng)裙","羽絨服"
        ];

        var app = new Vue({
            el:'#app',
            data:{
                temperature:14,
                cloth:'夾克長(zhǎng)裙',
            },
            methods:{
                add:function(){
                    this.temperature += 5;
                },
                less:function(){
                    this.temperature -= 5;
                }
            },
            //watch寫(xiě)在構(gòu)造器內(nèi)部
            //watch:{
                // temperature:function(newVal,oldVal){
                //     if(newVal >= 26){
                //         this.cloth = clothsArr[0];
                //     }else if(newVal < 26 && newVal >0){
                //         this.cloth = clothsArr[1];
                //     }else{
                //         this.cloth = clothsArr[2];
                //     }
                // }
            //}
        })
        //watch寫(xiě)在構(gòu)造器外部
        app.$watch('temperature',function(newVal,oldVal){
                    if(newVal >= 26){
                        this.cloth = clothsArr[0];
                    }else if(newVal < 26 && newVal >0){
                        this.cloth = clothsArr[1];
                    }else{
                        this.cloth = clothsArr[2];
                    }
                });
    </script>
</body>

5.mixins混入

<body>
    <h1>mixins option<h1>
    <hr>
    <div id="app">
        <p>{{num}}</p>
        <p><button @click="add()">增加1</button></p>
    </div>

    <script type="text/javascript">
        var addConsole = {
            updated:function(){
                //生命周期
                console.log('數(shù)據(jù)發(fā)生變化,變成了'+this.num);
            }
        };

        Vue.mixin({
            updated:function(){
                console.log('我是全局的混入');//最先執(zhí)行1
            }
        });

        var app = new Vue({
            el:'#app',
            data:{
                num:1,
            },
            methods:{
                add:function(){
                    this.num ++;
                }
            },
            updated:function(){
                console.log('我是原生的updated');//后執(zhí)行3
            },
            //混入的先執(zhí)行2
            mixins:[addConsole]
        })
    </script>
</body>

6.extends擴(kuò)展

<body>
    <div id="app">
        <p>${num}</p>
        <p><button @click="add()">增加1</button></p>
    </div>

    <script type="text/javascript">

        var extendObj = {
            updated:function(){
                console.log('我是擴(kuò)展的updated方法');
            },
            methods:{//方法名一樣,只執(zhí)行原生的方法。
                add:function(){
                    console.log('我是擴(kuò)展出來(lái)的的方法add');
                    this.num ++;
                }
            }
        }

        var app = new Vue({
            el:'#app',
            data:{
                num:1,
            },
            methods:{
                add:function(){
                    console.log('我是原生的方法add');
                    this.num ++;
                }
            },

            updated:function(){
                console.log('我是原生的updated');
            },

            extends:extendObj,//只能有1個(gè)擴(kuò)展

            delimiters:['${','}']//差值形式{{}}改成 ${} 
        })
    </script>
</body>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容