Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                h1 為什麼畫面沒有隨資料更新 — 深入 Vue 響應式原理筆記(物件部分)
hr
p.quotation 此篇文章為整理自 #[a(href="https://vuejs.org/v2/guide/reactivity.html") Vue 官網 Reactivity in Depth] 之筆記

h2 物件部分
hr
h3 畫面不會隨資料更新
#unregister-object(v-cloak)
    button(@click="addCount('dog')") addDogs
    button(@click="addCount('cat')") addCats
    button(@click="addCount('penguin')") addPenguin
    p dog {{ counter.dog }}, cat {{ counter.cat }}, penguin {{ counter.penguin }}
 
#register-object(v-cloak)
    h3 將需要響應式的資料都設定在 data 中
    button(@click="addCount('dog')") addDogs
    button(@click="addCount('cat')") addCats
    button(@click="addCount('penguin')") addPenguin
    p dog {{ counter.dog }}, cat {{ counter.cat }}, penguin {{ counter.penguin }}   



#dynamic-set-reactive-property(v-cloak)
    h3 動態添加響應式屬性(vm.$set)
    button(@click="addCount('dog')") addDogs
    button(@click="addCount('cat')") addCats
    button(@click="addCount('penguin')") addPenguin
    p dog {{ counter.dog }}, cat {{ counter.cat }}, penguin {{ counter.penguin }}   

#create-new-object(v-cloak)
    h3 建立一個新的物件(Object.assign())
    button(@click="addCount('dog')") addDogs
    button(@click="addCount('cat')") addCats
    button(@click="addCount('penguin')") addPenguin
    p dog {{ counter.dog }}, cat {{ counter.cat }}, penguin {{ counter.penguin }}   
    
h2 陣列部分
hr
              
            
!

CSS

              
                $green: #41CEC0;
$vue: #41b883;
$blue: #35495e;
$purple: #A53860;
$red: #C63647;
$yellow: #FFDF2C;
$orange: #E96900;

[v-cloak]{
  display: none;
}


              
            
!

JS

              
                /**
 * unregistaer-object
 **/

new Vue({
    el: "#unregister-object",
    data: {
        // 一開始沒有註冊 penguine,只註冊了 dog 和 cat
        counter: {
            dog: 0,
            cat: 0
        }
    },
    methods: {
        addCount(name) {
            this.counter[name]++;
        }
    },
    created() {
        // 在 created 的時候才建立 penguine 順便設值為 0
        this.counter.penguin = 0;
    },
    updated() {
        // 讓我們可以知道組件有被更新
        console.log("view updated");
    }
});

/**
 * register-object
 **/
new Vue({
    el: "#register-object",
    data: {
        counter: {
            dog: 0,
            cat: 0,
            penguin: 0 // 一開始就把要響應式變化的資料設定進來
        }
    },
    methods: {
        addCount(name) {
            this.counter[name]++;
        }
    },
    created() {
        // 在 created 的時候才建立 penguine 順便設值為 0
        this.penguin = 0;
    },
    updated() {
        // 讓我們可以知道組件有被更新
        console.log("view updated");
    }
});

/**
 * dynamic-set-reactive-property
 **/
new Vue({
    el: "#dynamic-set-reactive-property",
    data: {
        counter: {
            // 一開始沒有註冊 penguine,只註冊了 dog 和 cat
            dog: 0,
            cat: 0
        }
    },
    methods: {
        addCount(name) {
            this.counter[name]++;
        }
    },
    created() {
        // 在 created 的時候才建立 penguine 順便設值為 0
        // 使用 $set 動態新增響應式組件
        this.$set(this.counter, "penguin", 0);
    },
    updated() {
        // 讓我們可以知道組件有被更新
        console.log("view updated");
    }
});



/**
 * create-new-object
 **/
new Vue({
    el: "#create-new-object",
    data: {
        counter: {
            // 一開始沒有註冊 penguine,只註冊了 dog 和 cat
            dog: 0,
            cat: 0
        }
    },
    methods: {
        addCount(name) {
            this.counter[name]++;
        }
    },
    created() {
        // 透過建立新的物件來新增物件的屬性
        this.counter = Object.assign({}, this.counter, {
            penguin: 0,
            dog: 5
        })
    },
    beforeUpdate(){
        console.log(this.$el.querySelector('p').textContent)
        console.log("view beforeUpdate");
        this.$nextTick(function(){
            console.log(this.$el.querySelector('p').textContent)
        })
    },
    updated() {
        // 讓我們可以知道組件有被更新
        console.log("view updated");
    }
});
              
            
!
999px

Console