测试代码
- index.vue
<template>
<div class='box'>
demo
</div>
</template>
<script lang='ts' setup>
import { ref, reactive, computed, onMounted, nextTick, PropType } from 'vue';
import useRouterHook from './hooks/helper';
const router = useRouterHook();
</script>
<style lang='scss' scoped></style>
- hook 函数
//写hook,使用router
import { useRouter } from 'vue-router';
export default function useRouterHook() {
const router = useRouter();
console.log(router, 'router沙发斯蒂芬斯蒂芬谁');
onMounted(() => {
const router = useRouter();
console.log(router, 'sdfsdfsdfsdf');
//router 可以在hook中使用
/* setTimeout(() => {
console.log(router, 'sdfsdfsdfsdf2');
router.push('/pageN');
}, 3000); */
/* router.replace('/pageN'); */
});
return router;
}
前端工程师、程序员