File tree 5 files changed +135
-0
lines changed
5 files changed +135
-0
lines changed Original file line number Diff line number Diff line change
1
+ <template >
2
+ <!-- vue3的组件模版结构可以没有根标签-->
3
+ <button @click =" isShowDemo = !isShowDemo" >{{ isShowDemo ? '隐藏' : '显示'}}</button >
4
+ <Demo v-if =" isShowDemo" />
5
+ <hr />
6
+ <Test />
7
+ </template >
8
+
9
+ <script >
10
+ import Demo from " ./components/Demo" ;
11
+ import Test from " ./components/Test" ;
12
+ import { ref } from " vue" ;
13
+ export default {
14
+ name: ' App' ,
15
+ components: {Demo, Test},
16
+ setup (){
17
+ let isShowDemo = ref (true );
18
+ return {
19
+ isShowDemo
20
+ }
21
+ }
22
+ }
23
+ </script >
24
+
25
+ <style >
26
+ #app {
27
+ font-family : Avenir, Helvetica , Arial , sans-serif ;
28
+ -webkit-font-smoothing : antialiased ;
29
+ -moz-osx-font-smoothing : grayscale ;
30
+ text-align : center ;
31
+ color : #2c3e50 ;
32
+ margin-top : 60px ;
33
+ }
34
+ </style >
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <h1 >当前求和为:{{ sum }}</h1 >
3
+ <button @click =" sum++" >点我加一</button >
4
+ <hr />
5
+ <h2 >当前点击时鼠标的坐标为x:{{ point.x }}, y:{{ point.y }}</h2 >
6
+ </template >
7
+
8
+ <script >
9
+ import {ref } from ' vue' ;
10
+ import usePoint from " ../hooks/usePoint" ;
11
+ export default {
12
+ name: ' Demo' ,
13
+ setup (){
14
+ let sum = ref (0 );
15
+
16
+ // 复用自定义hooks
17
+ const point = usePoint ();
18
+
19
+ // 返回一个对象
20
+ return {
21
+ sum,
22
+ point
23
+ }
24
+ },
25
+ }
26
+ </script >
27
+
28
+ <style >
29
+ </style >
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <h2 >我是test组件</h2 >
3
+ <h3 >{{ point }}</h3 >
4
+ </template >
5
+
6
+ <script >
7
+ import usePoint from " ../hooks/usePoint" ;
8
+
9
+ export default {
10
+ name: " Test" ,
11
+ setup (){
12
+ const point = usePoint (); // 复用打点hook(封装数据以及其要用的生命周期钩子) 组合式api的好处
13
+ return {
14
+ point
15
+ }
16
+ }
17
+ }
18
+ </script >
19
+
20
+ <style scoped>
21
+
22
+ </style >
Original file line number Diff line number Diff line change
1
+ ///得到鼠标点的api
2
+
3
+ import { reactive , onMounted , onBeforeUnmount } from "vue" ;
4
+
5
+ export default function usePoint ( ) {
6
+ //响应式数据
7
+ let point = reactive ( {
8
+ x : 0 ,
9
+ y : 0
10
+ } ) ;
11
+
12
+ //方法
13
+ const savePoint = event => {
14
+ console . log ( event . pageX , event . pageY ) ;
15
+ point . x = event . pageX ;
16
+ point . y = event . pageY ;
17
+ } ;
18
+
19
+ //生命周期
20
+ onMounted ( ( ) => {
21
+ window . addEventListener ( 'click' , savePoint )
22
+ } ) ;
23
+
24
+ onBeforeUnmount ( ( ) => {
25
+ //在卸载之前取消事件的监听
26
+ window . removeEventListener ( 'click' , savePoint ) ;
27
+ } ) ;
28
+
29
+ return point ;
30
+ }
Original file line number Diff line number Diff line change
1
+ //引入不再是Vue构造函数了,引入的是一个名为createApp的工厂函数
2
+ //注意在这里无法像vue2那样去书写了,(这里并不兼容)
3
+ import { createApp } from 'vue'
4
+ import App from './App.vue'
5
+
6
+ //创建应用实例对象---app类似于vue2中vm,但app比vm更轻
7
+ const app = createApp ( App ) ;
8
+ // console.log(app);
9
+ //挂载
10
+ app . mount ( '#app' ) ;
11
+
12
+ // setTimeout(() => {
13
+ // app.unmount("#app");
14
+ // },2000)
15
+
16
+ // //vue2写法
17
+ // const vm = new Vue({
18
+ // render: h=> h(App)
19
+ // });
20
+ // vm.$mount('#app')
You can’t perform that action at this time.
0 commit comments