返回

在面试中脱颖而出:Vue 前端工程师必备知识点

前端

作为一名 Vue 前端工程师,面试是职业生涯中必不可少的一部分。为了帮助你在这场竞争中脱颖而出,我们整理了一系列常见的面试题,涵盖了框架原理、组件通信、性能优化等核心知识点。通过学习这些问题,你可以加深对 Vue 的理解,并在面试中展现出你的专业素养。

1. 自己封装一个指定的派发功能,派发指定组件的方法(如 elementUI 的 $dispatch 实现)

实现 $dispatch 方法:

// 通过 Vue.prototype 扩展
Vue.prototype.$dispatch = function (componentName, eventName, ...args) {
  const parent = this.$parent;
  while (parent) {
    if (parent.$options.name === componentName) {
      parent.$emit(eventName, ...args);
      break;
    }
    parent = parent.$parent;
  }
};

// 通过 eventBus 实现
const eventBus = new Vue();

// 子组件派发事件
this.$emit('some-event', data);

// 父组件监听事件
eventBus.$on('some-event', (data) => {
  // 处理数据
});

实现 $broadcast 方法:

// 通过 Vue.prototype 扩展
Vue.prototype.$broadcast = function (componentName, eventName, ...args) {
  const children = this.$children;
  for (let i = 0; i < children.length; i++) {
    const child = children[i];
    if (child.$options.name === componentName) {
      child.$emit(eventName, ...args);
    } else {
      child.$broadcast(componentName, eventName, ...args);
    }
  }
};

// 通过 eventBus 实现
const eventBus = new Vue();

// 父组件派发事件
eventBus.$emit('some-event', data);

// 子组件监听事件
eventBus.$on('some-event', (data) => {
  // 处理数据
});

2. Vue 修饰符 s

Vue 修饰符 s 用于简写 .sync 语法。它可以将子组件中数据的变化同步到父组件中。例如:

<child-component v-model="counter" />

等同于:

<child-component :counter.sync="counter" />

修饰符 s 也可以用于自定义指令中。例如:

Vue.directive('focus', {
  bind(el) {
    el.focus();
  }
});

等同于:

Vue.directive('focus', {
  bind(el, binding) {
    binding.value && el.focus();
  }
});

3. Vue 中的插槽

Vue 插槽允许父组件向子组件传递内容。它通过在父组件模板中定义插槽,然后在子组件模板中使用 <slot> 元素来实现。例如:

<!-- 父组件 -->
<template>
  <div>
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>

<!-- 子组件 -->
<template>
  <div>
    <header>
      <slot name="header"></slot>
    </header>
    <main>
      <slot></slot>
    </main>
    <footer>
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

在父组件中,我们可以通过如下方式传递内容到插槽:

<child-component>
  <template v-slot:header>
    <h1>Header</h1>
  </template>

  <template v-slot>
    <p>Content</p>
  </template>

  <template v-slot:footer>
    <p>Footer</p>
  </template>
</child-component>

4. Vue 中的 mixin

Vue mixin 允许我们在多个组件之间共享代码。它通过创建一个包含要共享代码的对象,然后在其他组件中使用 mixins 选项来实现。例如:

// mixin.js
export default {
  data() {
    return {
      count: 0
    };
  },
  methods: {
    increment() {
      this.count++;
    }
  }
};

// component1.vue
import mixin from './mixin.js';

export default {
  mixins: [mixin],
  template: `<button @click="increment">+</button>`
};

// component2.vue
import mixin from './mixin.js';

export default {
  mixins: [mixin],
  template: `<p>Count: {{ count }}</p>`
};

5. Vue 中的生命周期钩子

Vue 生命周期钩子允许我们在组件的不同生命周期阶段执行特定的任务。这些钩子包括:

  • beforeCreate:在实例初始化之后,数据观测和事件配置之前被调用。
  • created:在实例创建完成之后被调用。
  • beforeMount:在挂载开始之前被调用。
  • mounted:在挂载完成后被调用。
  • beforeUpdate:在数据更新之前被调用。
  • updated:在数据更新完成后被调用。
  • beforeDestroy:在实例销毁之前被调用。
  • destroyed:在实例销毁之后被调用。

我们可以在组件中使用这些钩子来执行各种操作,例如:

  • created 钩子中初始化数据。
  • mounted 钩子中获取 DOM 元素的引用。
  • updated 钩子中更新 DOM 元素的内容。
  • destroyed 钩子中释放资源。