feat: Add Vue3 exercises and interview plan
- Introduced Vue3 exercises covering composable API, reactivity, lifecycle hooks, and built-in components. - Added structured interview plan for frontend candidates focusing on HTML, CSS, JavaScript, TypeScript, and Vue. - Included starter files for each exercise and detailed README documentation for guidance.
This commit is contained in:
24
08-vue3/11-before-hooks-and-expose/README.md
Normal file
24
08-vue3/11-before-hooks-and-expose/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# 练习 11:before 系列生命周期和 expose
|
||||
|
||||
## 目标
|
||||
|
||||
学会在组合式 API 中使用 before 系列生命周期,并理解子组件如何有选择地暴露能力给父组件。
|
||||
|
||||
## 你要练什么
|
||||
|
||||
- `onBeforeMount`
|
||||
- `onBeforeUpdate`
|
||||
- `onBeforeUnmount`
|
||||
- `expose`
|
||||
|
||||
## 任务
|
||||
|
||||
- 在不同生命周期里输出日志
|
||||
- 父组件通过模板 `ref` 获取子组件实例
|
||||
- 子组件通过 `expose` 暴露一个 `focusInput` 方法
|
||||
- 父组件点击按钮后调用这个暴露出来的方法
|
||||
|
||||
## 文件
|
||||
|
||||
- [starter.html](/Users/lijiaqing/home/wwwroot/front-end-example/08-vue3/11-before-hooks-and-expose/starter.html)
|
||||
- [starter.js](/Users/lijiaqing/home/wwwroot/front-end-example/08-vue3/11-before-hooks-and-expose/starter.js)
|
||||
25
08-vue3/11-before-hooks-and-expose/starter.html
Normal file
25
08-vue3/11-before-hooks-and-expose/starter.html
Normal file
@@ -0,0 +1,25 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>before 系列生命周期和 expose</title>
|
||||
<style>
|
||||
body { margin: 0; padding: 32px; font-family: "PingFang SC", sans-serif; background: #f6f8fc; }
|
||||
.panel { max-width: 760px; margin: 0 auto; padding: 24px; border-radius: 18px; background: #fff; border: 1px solid #dde5f2; }
|
||||
button, input { padding: 12px 14px; border-radius: 12px; font: inherit; }
|
||||
input { width: 100%; border: 1px solid #ccd7e9; margin-top: 12px; }
|
||||
button { border: 0; background: #2d6cdf; color: #fff; cursor: pointer; margin-right: 10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<section id="app" class="panel">
|
||||
<button type="button" @click="focusChildInput">聚焦子组件输入框</button>
|
||||
<button type="button" @click="showChild = !showChild">切换子组件显示</button>
|
||||
<child-panel v-if="showChild" ref="childPanel"></child-panel>
|
||||
</section>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
|
||||
<script src="./starter.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
59
08-vue3/11-before-hooks-and-expose/starter.js
Normal file
59
08-vue3/11-before-hooks-and-expose/starter.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const {
|
||||
createApp,
|
||||
ref,
|
||||
onBeforeMount,
|
||||
onBeforeUpdate,
|
||||
onBeforeUnmount,
|
||||
} = Vue;
|
||||
|
||||
createApp({
|
||||
components: {
|
||||
ChildPanel: {
|
||||
template: `
|
||||
<div>
|
||||
<p>我是子组件</p>
|
||||
<input ref="inputEl" type="text" placeholder="等待父组件调用 focus" />
|
||||
</div>
|
||||
`,
|
||||
setup(props, { expose }) {
|
||||
const inputEl = ref(null);
|
||||
|
||||
onBeforeMount(() => {
|
||||
// 任务:输出 beforeMount 日志
|
||||
});
|
||||
|
||||
onBeforeUpdate(() => {
|
||||
// 任务:输出 beforeUpdate 日志
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
// 任务:输出 beforeUnmount 日志
|
||||
});
|
||||
|
||||
function focusInput() {
|
||||
// 任务:聚焦 inputEl
|
||||
}
|
||||
|
||||
// 任务:通过 expose 暴露 focusInput
|
||||
|
||||
return {
|
||||
inputEl,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
setup() {
|
||||
const showChild = ref(true);
|
||||
const childPanel = ref(null);
|
||||
|
||||
function focusChildInput() {
|
||||
// 任务:调用 childPanel.value 暴露出来的方法
|
||||
}
|
||||
|
||||
return {
|
||||
showChild,
|
||||
childPanel,
|
||||
focusChildInput,
|
||||
};
|
||||
},
|
||||
}).mount("#app");
|
||||
Reference in New Issue
Block a user