Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 712b98d

Browse files
committedOct 13, 2021
vue3自定义hook函数
1 parent d06d19a commit 712b98d

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
 

‎12_src_自定义hook函数/App.vue

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

‎12_src_自定义hook函数/main.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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')

0 commit comments

Comments
 (0)
Please sign in to comment.