diff --git a/20_脚手架/vue3_test/12.src_自定义hook/App.vue b/20_脚手架/vue3_test/12.src_自定义hook/App.vue new file mode 100644 index 0000000..523ca4e --- /dev/null +++ b/20_脚手架/vue3_test/12.src_自定义hook/App.vue @@ -0,0 +1,22 @@ + + + diff --git a/20_脚手架/vue3_test/12.src_自定义hook/assets/logo.png b/20_脚手架/vue3_test/12.src_自定义hook/assets/logo.png new file mode 100644 index 0000000..f3d2503 Binary files /dev/null and b/20_脚手架/vue3_test/12.src_自定义hook/assets/logo.png differ diff --git a/20_脚手架/vue3_test/12.src_自定义hook/components/demo.vue b/20_脚手架/vue3_test/12.src_自定义hook/components/demo.vue new file mode 100644 index 0000000..ccdba68 --- /dev/null +++ b/20_脚手架/vue3_test/12.src_自定义hook/components/demo.vue @@ -0,0 +1,26 @@ + + + diff --git a/20_脚手架/vue3_test/12.src_自定义hook/hooks/usePoint.js b/20_脚手架/vue3_test/12.src_自定义hook/hooks/usePoint.js new file mode 100644 index 0000000..c91624c --- /dev/null +++ b/20_脚手架/vue3_test/12.src_自定义hook/hooks/usePoint.js @@ -0,0 +1,24 @@ +import {onBeforeUnmount, onMounted, reactive} from "vue"; + +export default function() { + let point = reactive({ + x: 0, + y: 0 + }) + + function savePoint() { + console.log(event.pageX, event.pageY) + point.x = event.pageX + point.y = event.pageY + } + + onMounted(() => { + window.addEventListener('click', savePoint) + }) + + onBeforeUnmount(() => { + window.removeEventListener('click', savePoint) + }) + + return point +} diff --git a/20_脚手架/vue3_test/12.src_自定义hook/main.js b/20_脚手架/vue3_test/12.src_自定义hook/main.js new file mode 100644 index 0000000..8a90e5c --- /dev/null +++ b/20_脚手架/vue3_test/12.src_自定义hook/main.js @@ -0,0 +1,10 @@ +// 引入的不再是Vue构造函数, 引入的是一个名为createApp的工厂函数 +import { createApp } from 'vue' +import App from './App.vue' + +// createApp(App).mount('#app') + +// 创建应用实例对象 +const app = createApp(App) +console.log('@@@',app) +app.mount('#app') \ No newline at end of file diff --git a/20_脚手架/vue3_test/src/components/demo.vue b/20_脚手架/vue3_test/src/components/demo.vue index 2070484..ccdba68 100644 --- a/20_脚手架/vue3_test/src/components/demo.vue +++ b/20_脚手架/vue3_test/src/components/demo.vue @@ -1,10 +1,13 @@ diff --git a/20_脚手架/vue3_test/src/hooks/usePoint.js b/20_脚手架/vue3_test/src/hooks/usePoint.js new file mode 100644 index 0000000..c91624c --- /dev/null +++ b/20_脚手架/vue3_test/src/hooks/usePoint.js @@ -0,0 +1,24 @@ +import {onBeforeUnmount, onMounted, reactive} from "vue"; + +export default function() { + let point = reactive({ + x: 0, + y: 0 + }) + + function savePoint() { + console.log(event.pageX, event.pageY) + point.x = event.pageX + point.y = event.pageY + } + + onMounted(() => { + window.addEventListener('click', savePoint) + }) + + onBeforeUnmount(() => { + window.removeEventListener('click', savePoint) + }) + + return point +}