VUE----computed & watch

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

<head>
    <meta charset="UFT-8">
    <title>
        vue
    </title>
    <script src="https://cdn.bootcss.com/vue/2.6.11/vue.min.js"></script>
</head>


<!-- 
    計算屬性: computed
    偵聽器: watch
    使用場景: watch(異步場景) computed(數據聯動)
 -->
<body>
    <div id="app">
        {{msg}}
        <p>
            {{msg1}}
        </p>
    </div>

    <script>
        var arr = 'new test'

        // 將vue賦值給一個變量就可以在控制檯調試vue
        var app = new Vue({
            el: '#app',
            data: {
                msg: 'hello vue!',
                another: 'another Hello Vue!'
            },
            methods: {

            },
            watch: {
                msg: function (newval, oldval) {
                    console.log('newval is ' + newval)
                    console.log('oldval is ' + oldval)
                }
            },
            computed: {
                msg1: function () {
                    // 由 msg + another 組成的 msg1 在 vue實例中任意一個值變化 都會影響到msg1 
                    return 'computed:' + this.msg + ',' + this.another + arr
                }
            },

        })
    </script>
</body>
</html>

 

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