File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < title > data_methods_computed总结</ title >
6
+ < script type ="text/javascript " src ="../js/vue.js "> </ script >
7
+ </ head >
8
+ < body >
9
+ <!-- 准备好一个容器-->
10
+ < div id ="root ">
11
+ < h2 > {{x}}</ h2 >
12
+ < h2 > {{y()}}</ h2 >
13
+ < h2 > {{z}}</ h2 >
14
+ < button @click ="y "> 点我调用y方法</ button > < br /> < br />
15
+ < button @click ="y() "> 点我调用y方法</ button > < br /> < br />
16
+ 展示x的值:< input type ="text " v-model ="x ">
17
+ </ div >
18
+
19
+ < script type ="text/javascript " >
20
+ const vm = new Vue ( {
21
+ el :'#root' , //指定vm为哪个容器服务
22
+ data :{ //驱动页面显示的数据都放在这里
23
+ x :100 //x最终会通过数据代理的方式放在vm身上
24
+ } ,
25
+ methods :{ //所有用到的函数都配置在这里
26
+ y ( ) { //y直接被放在vm身上
27
+ console . log ( 'y被调用了' )
28
+ return 200
29
+ }
30
+ } ,
31
+ computed :{
32
+ z :{ //z直接被放在vm身上了
33
+ set ( value ) { //z的值被改变时,set执行,set中的this是vm,set会收到修改的值
34
+ console . log ( '有人修改z了,修改的值为:' , value )
35
+ } ,
36
+ get ( ) { //z的值被读取时,或z依赖的值发生变化时,get执行,get中的this是vm,前提是:页面中要用到z
37
+ console . log ( 'get被调用了' )
38
+ return this . x * 1 + 1
39
+ }
40
+ }
41
+ }
42
+ } )
43
+
44
+ console . log ( vm )
45
+ </ script >
46
+ </ body >
47
+ </ html >
You can’t perform that action at this time.
0 commit comments