|
| 1 | +import Vue from 'vue' |
| 2 | + |
| 3 | +const app = new Vue({ |
| 4 | + // el: '#root', |
| 5 | + template: '<div ref="div">{{text}} {{obj.a}}</div>', |
| 6 | + data: { |
| 7 | + text: 0, |
| 8 | + obj: {} |
| 9 | + } |
| 10 | + // watch: { |
| 11 | + // text (newText, oldText) { |
| 12 | + // console.log(`${newText} : ${oldText}`) |
| 13 | + // } |
| 14 | + // } |
| 15 | +}) |
| 16 | + |
| 17 | +app.$mount('#root') // 效果和 el:'#root' 相同 |
| 18 | +console.log(app.$el) |
| 19 | + |
| 20 | +console.log(app.$data) |
| 21 | +console.log(app.$props) |
| 22 | +console.log(app.$options) |
| 23 | +setInterval(() => { |
| 24 | + app.$options.data.text += 1 // 不变化 |
| 25 | + app.$data.text += 1 // 变化,app.text代理到app.$data.text |
| 26 | +}, 1000) |
| 27 | + |
| 28 | +app.$options.render = (h) => { |
| 29 | + return h('div', {}, 'new render function') |
| 30 | +} |
| 31 | + |
| 32 | +console.log(app.$root === app) // true |
| 33 | +console.log(app.$children) |
| 34 | +console.log(app.$slots) |
| 35 | +console.log(app.$scopedSlots) |
| 36 | +console.log(app.$refs) |
| 37 | +console.log(app.$isServer) |
| 38 | + |
| 39 | +// watch及解除 |
| 40 | +const unWatch = app.$watch('text', (newText, oldText) => { |
| 41 | + console.log(`${newText} : ${oldText}`) |
| 42 | +}) |
| 43 | +setTimeout(() => { |
| 44 | + unWatch() |
| 45 | +}, 2000) |
| 46 | + |
| 47 | +// 事件触发 |
| 48 | +app.$on('test1', (a, b) => { |
| 49 | + console.log(`test emited ${a} ${b}`) |
| 50 | +}) |
| 51 | +app.$once('test2', (a, b) => { |
| 52 | + console.log(`test emited ${a} ${b}`) |
| 53 | +}) |
| 54 | +setInterval(() => { |
| 55 | + app.$emit('test1', 1, 2) |
| 56 | + app.$emit('test2', 1, 2) |
| 57 | +}, 1000) |
| 58 | + |
| 59 | +// forceUpdate & set |
| 60 | +let i = 0 |
| 61 | +setInterval(() => { |
| 62 | + app.obj.a = i++ |
| 63 | + app.$forceUpdate() // 对没有没有初始声明的值强制渲染 |
| 64 | + // app.$set(app.obj, 'a', i) // 补上声明,效果与$forceUpdate()相同 |
| 65 | + // app.$delete ... |
| 66 | +}, 1000) |
| 67 | + |
| 68 | +// nextTick |
| 69 | +// app.$nextTick([callback]) |
0 commit comments