《前后端面试题
》专栏集合了前后端各个知识模块的面试题,包括html,javascript,css,vue,react,java,Openlayers,leaflet,cesium,mapboxGL,threejs,nodejs,mangoDB,MySQL,Linux... 。

文章目录
- 一、本文面试题目录
-
-
- [31. Vue 3中如何实现表单双向绑定?](#31. Vue 3中如何实现表单双向绑定?)
- [32. Vue 3的响应式系统相比Vue 2有哪些性能提升?](#32. Vue 3的响应式系统相比Vue 2有哪些性能提升?)
- [33. Vue 3中如何实现路由懒加载?](#33. Vue 3中如何实现路由懒加载?)
- [34. Vue 3中如何处理异步操作?](#34. Vue 3中如何处理异步操作?)
- [35. Vue 3中如何进行单元测试?](#35. Vue 3中如何进行单元测试?)
- [36. Vue 3中如何使用Vuex?](#36. Vue 3中如何使用Vuex?)
- [37. Vue 3中如何实现插件?](#37. Vue 3中如何实现插件?)
- [38. Vue 3中如何使用自定义过滤器?](#38. Vue 3中如何使用自定义过滤器?)
- [39. Vue 3中如何实现服务端渲染(SSR)?](#39. Vue 3中如何实现服务端渲染(SSR)?)
- [40. Vue 3中如何优化大型应用的性能?](#40. Vue 3中如何优化大型应用的性能?)
- [41. Vue 3中如何实现国际化(i18n)?](#41. Vue 3中如何实现国际化(i18n)?)
- [42. Vue 3中如何处理错误边界?](#42. Vue 3中如何处理错误边界?)
- [43. Vue 3中如何实现组件通信?](#43. Vue 3中如何实现组件通信?)
- [44. Vue 3中如何使用v-memo优化渲染性能?](#44. Vue 3中如何使用v-memo优化渲染性能?)
- [45. Vue 3中如何使用VueUse?](#45. Vue 3中如何使用VueUse?)
-
- 二、120道面试题目录列表
一、本文面试题目录
31. Vue 3中如何实现表单双向绑定?
在Vue 3中,可以使用v-model
指令实现表单双向绑定。对于基础组件,它是v-bind
和v-on
的语法糖。例如:
html
<template>
<input v-model="message" type="text" />
<p>{{ message }}</p>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('');
</script>
对于自定义组件,需要通过modelValue
prop和update:modelValue
事件实现:
html
<!-- 父组件 -->
<template>
<CustomInput v-model="message" />
</template>
<!-- 子组件 -->
<template>
<input :value="modelValue" @input="emit('update:modelValue', $event.target.value)" />
</template>
<script setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
modelValue: String
});
const emits = defineEmits(['update:modelValue']);
</script>
32. Vue 3的响应式系统相比Vue 2有哪些性能提升?
Vue 3的响应式系统使用Proxy替代Object.defineProperty,带来以下性能提升:
- 深度监听优化:Proxy可以拦截对象的所有操作,无需递归遍历所有属性。
- 动态属性支持 :可以检测到对象属性的添加和删除,无需使用
Vue.set
/Vue.delete
。 - 更好的数组支持 :可以拦截数组的原生方法,如
push
、pop
等。 - 懒处理:只有在访问嵌套属性时才会创建响应式代理,减少初始开销。
33. Vue 3中如何实现路由懒加载?
Vue 3中推荐使用动态导入实现路由懒加载:
javascript
import { createRouter, createWebHistory } from 'vue-router';
const routes = [
{
path: '/home',
name: 'Home',
component: () => import('../views/Home.vue') // 懒加载组件
},
{
path: '/about',
name: 'About',
component: () => import('../views/About.vue') // 懒加载组件
}
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
34. Vue 3中如何处理异步操作?
Vue 3中处理异步操作的方式有:
- async/await:在setup函数或生命周期钩子中使用async/await。
javascript
<script setup>
import { onMounted } from 'vue';
onMounted(async () => {
const data = await fetchData();
console.log(data);
});
</script>
- Suspense组件:处理异步组件加载状态。
html
<template>
<Suspense>
<template #default>
<AsyncComponent />
</template>
<template #fallback>
<div>Loading...</div>
</template>
</Suspense>
</template>
- 自定义组合式函数:封装异步逻辑。
javascript
// useFetch.js
import { ref, onMounted } from 'vue';
export function useFetch(url) {
const data = ref(null);
const error = ref(null);
const loading = ref(true);
const fetchData = async () => {
try {
const response = await fetch(url);
data.value = await response.json();
} catch (err) {
error.value = err;
} finally {
loading.value = false;
}
};
onMounted(fetchData);
return { data, error, loading };
}
35. Vue 3中如何进行单元测试?
Vue 3的单元测试可以使用以下工具和方法:
- @vue/test-utils:Vue官方测试工具库。
- Jest:JavaScript测试框架。
- Vitest:Vite原生的测试框架,速度更快。
示例测试代码:
javascript
import { describe, it, expect } from 'vitest';
import { shallowMount } from '@vue/test-utils';
import HelloWorld from './HelloWorld.vue';
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message';
const wrapper = shallowMount(HelloWorld, {
props: { msg }
});
expect(wrapper.text()).toContain(msg);
});
});
36. Vue 3中如何使用Vuex?
Vue 3中使用Vuex 4,需要创建store并通过createStore
函数初始化:
javascript
// store/index.js
import { createStore } from 'vuex';
const store = createStore({
state() {
return {
count: 0
};
},
mutations: {
increment(state) {
state.count++;
}
},
actions: {
incrementAsync({ commit }) {
setTimeout(() => {
commit('increment');
}, 1000);
}
},
getters: {
doubleCount(state) {
return state.count * 2;
}
}
});
export default store;
在main.js中安装store:
javascript
import { createApp } from 'vue';
import App from './App.vue';
import store from './store';
const app = createApp(App);
app.use(store);
app.mount('#app');
37. Vue 3中如何实现插件?
Vue 3中实现插件需要定义一个包含install
方法的对象:
javascript
// plugins/myPlugin.js
export const myPlugin = {
install(app, options) {
// 全局注册组件
app.component('MyComponent', {
template: '<div>Global Component</div>'
});
// 全局添加方法
app.config.globalProperties.$myMethod = () => {
console.log('This is a global method');
};
// 注入provide
app.provide('myPlugin', options);
}
};
在main.js中使用插件:
javascript
import { createApp } from 'vue';
import App from './App.vue';
import { myPlugin } from './plugins/myPlugin';
const app = createApp(App);
app.use(myPlugin, { option: 'value' });
app.mount('#app');
38. Vue 3中如何使用自定义过滤器?
Vue 3中移除了过滤器,推荐使用计算属性或方法替代:
javascript
// 计算属性替代
<script setup>
import { computed, ref } from 'vue';
const price = ref(100);
const formattedPrice = computed(() => {
return `$${price.value.toFixed(2)}`;
});
</script>
<template>
<div>{{ formattedPrice }}</div>
</template>
或使用全局方法:
javascript
// main.js
app.config.globalProperties.$formatPrice = (price) => {
return `$${price.toFixed(2)}`;
};
// 模板中使用
<template>
<div>{{ $formatPrice(price) }}</div>
</template>
39. Vue 3中如何实现服务端渲染(SSR)?
Vue 3的SSR可以通过以下方式实现:
- Vue CLI + @vue/server-renderer:手动配置SSR环境。
- Nuxt 3:官方推荐的Vue 3 SSR框架。
- Vite + createSSRApp:使用Vite的SSR支持。
基本步骤:
- 创建Vue应用实例:
javascript
// src/main.js
import { createSSRApp } from 'vue';
import App from './App.vue';
export function createApp() {
const app = createSSRApp(App);
return { app };
}
- 服务端渲染逻辑:
javascript
// server.js
import { createApp } from './src/main.js';
import { renderToString } from '@vue/server-renderer';
server.get('*', async (req, res) => {
const { app } = createApp();
const html = await renderToString(app);
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Vue 3 SSR</title>
</head>
<body>
<div id="app">${html}</div>
<script src="/client.js"></script>
</body>
</html>
`);
});
40. Vue 3中如何优化大型应用的性能?
优化Vue 3大型应用性能的方法:
- 路由懒加载:将路由组件分割成多个小文件,按需加载。
- 组件懒加载 :使用
defineAsyncComponent
动态加载非关键组件。 - 虚拟列表:处理大量数据渲染时,只渲染可视区域的内容。
- Tree Shaking:利用Vite/Webpack的Tree Shaking功能,移除未使用的代码。
- 缓存组件 :使用
keep-alive
缓存频繁切换的组件。 - 减少响应式对象大小:避免将大型对象或数组定义为响应式数据。
- 使用v-memo/v-bind:once:减少不必要的重新渲染。
No. | 大剑师精品GIS教程推荐 |
---|---|
0 | 地图渲染基础- 【WebGL 教程】 - 【Canvas 教程】 - 【SVG 教程】 |
1 | Openlayers 【入门教程】 - 【源代码+示例 300+】 |
2 | Leaflet 【入门教程】 - 【源代码+图文示例 150+】 |
3 | MapboxGL 【入门教程】 - 【源代码+图文示例150+】 |
4 | Cesium 【入门教程】 - 【源代码+综合教程 200+】 |
5 | threejs 【中文API】 - 【源代码+图文示例200+】 |
41. Vue 3中如何实现国际化(i18n)?
Vue 3中实现国际化可以使用vue-i18n
v9+:
javascript
// i18n.js
import { createI18n } from 'vue-i18n';
const messages = {
en: {
message: {
hello: 'Hello World'
}
},
zh: {
message: {
hello: '你好,世界'
}
}
};
const i18n = createI18n({
locale: 'en', // 默认语言
messages
});
export default i18n;
在main.js中安装:
javascript
import { createApp } from 'vue';
import App from './App.vue';
import i18n from './i18n';
const app = createApp(App);
app.use(i18n);
app.mount('#app');
在模板中使用:
html
<template>
<div>{{ $t('message.hello') }}</div>
</template>
42. Vue 3中如何处理错误边界?
Vue 3中可以使用app.config.errorHandler
全局捕获错误:
javascript
// main.js
const app = createApp(App);
app.config.errorHandler = (err, instance, info) => {
// 记录错误信息
console.error('Error:', err);
console.error('Component instance:', instance);
console.error('Error info:', info);
// 可以在这里显示错误提示组件
};
app.mount('#app');
也可以在组件中使用onErrorCaptured
生命周期钩子捕获子组件错误:
javascript
<script setup>
import { onErrorCaptured } from 'vue';
onErrorCaptured((err, instance, info) => {
// 处理错误
console.log('Error captured:', err);
// 返回false阻止错误继续向上传播
return false;
});
</script>
43. Vue 3中如何实现组件通信?
Vue 3中实现组件通信的方式有:
- props和$emit:父子组件通信的基本方式。
- 事件总线:创建全局事件总线,用于跨组件通信。
- provide/inject:跨层级组件通信。
- Vuex/Pinia:状态管理库,用于全局状态共享。
- 自定义事件 :使用
EventEmitter
实现自定义事件系统。 - Composition API:通过组合式函数共享逻辑和状态。
44. Vue 3中如何使用v-memo优化渲染性能?
v-memo
是Vue 3新增的指令,用于缓存渲染结果,避免不必要的重新渲染:
html
<!-- 仅当item.id或item.name变化时才重新渲染 -->
<div v-for="item in list" v-memo="[item.id, item.name]">
{{ item.name }}
</div>
注意:
v-memo
需要传入一个依赖数组。- 仅在性能敏感的场景使用,避免过度使用。
- 不要在依赖数组中包含复杂对象,应使用原始值。
45. Vue 3中如何使用VueUse?
VueUse是一个基于Vue 3 Composition API的实用函数集合。使用步骤:
- 安装:
npm install @vueuse/core
- 在组件中导入使用:
javascript
<script setup>
import { useMouse, useIntersectionObserver } from '@vueuse/core';
// 使用鼠标位置
const { x, y } = useMouse();
// 使用Intersection Observer
const target = ref(null);
const { isIntersecting } = useIntersectionObserver(target, {
threshold: 0.5
});
</script>
<template>
<div ref="target">
Mouse position: {{ x }}, {{ y }}
<div v-if="isIntersecting">元素可见</div>
</div>
</template>