Skip to content

Commit 9def454

Browse files
vue
1 parent b44cedb commit 9def454

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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])
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Vue from 'vue'
2+
3+
const app = new Vue({
4+
// el: '#root',
5+
// template: '<div>{{text}}</div>',
6+
data: {
7+
text: 0
8+
},
9+
beforeCreate () {
10+
console.log(this.$el, 'beforeCreate') // undefined
11+
},
12+
created () {
13+
console.log(this.$el, 'created') // undefined
14+
},
15+
// SSR不存在 ------------------------
16+
beforeMount () {
17+
console.log(this.$el, 'beforeMount') // 未填充数据的节点
18+
},
19+
mounted () {
20+
console.log(this.$el, 'mounted') // 完成渲染的节点
21+
},
22+
//----------------------------------
23+
beforeUpdate () {
24+
console.log(this, 'beforeUpdate')
25+
},
26+
updated () {
27+
console.log(this, 'updated')
28+
},
29+
// 与keep-alive相关 -----------------
30+
activated () {
31+
console.log(this, 'activated')
32+
},
33+
deactivated () {
34+
console.log(this, 'deactivated')
35+
},
36+
//----------------------------------
37+
beforeDestroy () {
38+
console.log(this, 'beforeDestroy')
39+
},
40+
destroyed () {
41+
console.log(this, 'destroyed')
42+
},
43+
//////////////////////////////////////////////////
44+
// 与使用template相同 在beforeMount和mounted之间执行
45+
render (h) {
46+
// throw new TypeError('render error') // 主动触发renderError
47+
console.log('render function invoked')
48+
return h('div', {}, this.text)
49+
},
50+
renderError (h, err) {
51+
return h('div', {}, err.stack)
52+
},
53+
errorCaptured () {
54+
// 会向上冒泡,并且正式环境可以使用
55+
}
56+
})
57+
58+
app.$mount('#root')
59+
// 触发update系列
60+
setInterval(() => {
61+
app.text = app.text += 1
62+
}, 1000)
63+
// 主动销毁,解除事件监听和watch
64+
setTimeout(() => {
65+
app.$destroy()
66+
}, 1000)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Vue from 'vue'
2+
3+
var globalVar = 'sunshine' // eslint-disable-line
4+
new Vue({
5+
el: '#root',
6+
// template: `
7+
// <div >
8+
// <p v-html="html"></p>
9+
// </div>
10+
// `,
11+
template: `
12+
<div
13+
v-bind:id="eleId" // :id
14+
v-on:click="handleClick" // @click
15+
:class="[{ active: isActive }]"
16+
:style="[styles, styles2]"
17+
>
18+
{{globalVar}} // 报错,不能访问外部变量
19+
<p v-html="html"></p>
20+
<p>{{getJoinedArr(arr)}}</p>
21+
</div>
22+
`,
23+
data: {
24+
isActive: false,
25+
arr: [1, 2, 3],
26+
html: '<span>123</span>',
27+
eleId: 'main',
28+
styles: {
29+
color: 'red',
30+
appearance: 'none' // 消除浏览器默认样式,浏览器需要前缀,自动添加前缀
31+
},
32+
styles2: {
33+
color: 'black'
34+
}
35+
},
36+
methods: {
37+
handleClick () {
38+
alert('clicked') // eslint-disable-line
39+
},
40+
getJoinedArr (arr) {
41+
return arr.join(' ')
42+
}
43+
}
44+
})

0 commit comments

Comments
 (0)