vue4-計算屬性

 

類裏有個computed和methods相識,但是computed裏的函數計算後會緩存結果,methods的每次都會重新計算。

在模板語法複雜時就把它寫入computed屬性中,起到分離複雜邏輯的作用(是屬性所以調用時不加())

 

 

<!DOCTYPE html>
<html>
    <head>
        
        <meta charset="utf-8">
        <title>vue1</title>
        <script src="vue.js"></script>
        <link rel="stylesheet" type="text/css" href="vue.css">
    
    </head>
    <body>
        
        
        <div id="app">
            <div id="test">{{test}}</div>
            <div v-html="chazhi"></div>
            <div>{{remsg}}</div>

            <div>
                <input type="text" v-model="msg">
                <button @click="submit">submit</button>
                <ul>
                    <li v-for="item of list">{{item}}</li>
                </ul>
            </div>
        </div>        
        <script>
            var app=new Vue({
                el:"#app",
                data:{
                    test:'我是test',
                    chazhi:`<div>chzhi context</div>`,
                    list:[],
                    msg:""
                
                },
                methods:{
                    demo:function(){

                    },
                    submit:function(){
                        this.list.push(this.msg),
                        this.msg=""
                    }

            

                },
                computed:{
                    remsg:function(){
                        return this.chazhi.split('').reverse().join('')
                    }

                },
                beforeCreate:function(){
                        console.log("我是掛載函數")
                    }
                
            })        
        </script>
    </body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章