10 Essential Vue.js Interview Questions *
Toptal sourced essential questions that the best Vue.js developers and engineers can answer. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.
Hire a Top Vue.js Developer NowInterview Questions
槽是子组件中的占位符,由父组件传递的内容填充. 常规槽的内容在父组件的作用域中编译,然后传递给子组件.
Thus you can’t use child component properties in a slot’s content. But scoped slots 允许您将子组件数据传递给父作用域,然后在槽内容中使用该数据.
All properties defined in a Vue instance’s data option are reactive这意味着如果它们发生变化,组件将自动更新并根据需要重新呈现.
在初始化期间,所有这些属性都转换为getter和setter, thus allowing Vue to detect when those properties are accessed or changed.
The following limitations must be accounted for when designing a Vue app:
- 由于JavaScript的限制,我们无法检测对象属性的添加或删除
Vue.set
method must be used to add new root-level reactive properties. - Similarly, Vue cannot detect when an array item is modified using an index.
Vue.set
must be used here as well.
Mixin支持是一个允许Vue中组件之间代码重用的特性.js application and a software composition tool.
mixin是一个JavaScript对象,它可以包含组件可以包含的任何选项. 当组件使用mixin时,所有的mixin内容都会与组件的选项合并.
Mixins help with following the DRY (don’t repeat yourself) principle. A mixin can even be applied globally to every component instance. In that case, it’s called a global mixin.
Mixins are a powerful tool, but some caution is needed while using them. 与所有注入代码一样,我们应该小心避免维护问题和意外行为.
它有助于使用纯函数来实现mixins,这些函数不修改自己作用域之外的任何东西.
Global mixins should be avoided, 随着应用程序的增长,影响每个组件可能会导致维护问题. 根据需要向组件中注入特定的mixins会带来更易于维护的代码.
Apply to Join Toptal's Development Network
and enjoy reliable, steady, remote Freelance Vue.js Developer Jobs
A single-file component is a file with a .vue
extension that contains a Vue component. 它将组件的模板、逻辑和样式都捆绑在一个文件中. It consists of one block, optional
and
blocks, and possible additional custom blocks.
To use one, 你需要设置Vue Loader来解析文件(通常作为webpack构建管道的一部分完成). 但这也支持使用非默认语言,如带有可插拔预处理器的Sass或HTML模板语言.
Vue.js uses what’s called a one-way data flow. 使用prop或自定义属性将数据从给定的父组件传递给子组件,这些属性将成为子组件实例的属性.
当父组件更新prop值时,它会在子组件中自动更新. Mutating a property inside a child component should not be done. 而且,它不会影响父组件(除非它是一个对象或数组)。.
The child component can communicate back to the parent via an event. 父组件可以为子组件实例发出的任何事件分配处理程序,并且可以将数据传递回父组件. 子组件可以发出一个特殊的事件来更新传递给它的道具.
Memory leaks in Vue.js应用程序通常来自于使用第三方库来创建自己的实例和/或操作DOM. The v-if
directive and the Vue Router destroy Vue component instances; however, cleaning up after any third party library should be done manually in the beforeDestroy()
lifecycle hook.
For example, let’s say we use a fictional library, PowerGraph.js, inside our component. It creates a graph instance that displays some data on the page:
mounted() {
this.chart = new PowerGraph();
}
We need to call the graph instance’s destroy()
method if it’s provided or implement our own cleanup method:
beforeDestroy() {
this.chart.destroy();
}
If cleanup is not done before our component gets destroyed, then that memory is never going to be released. Hence, a memory leak.
虚拟DOM是表示由Vue管理的DOM节点的JavaScript对象的树状数据结构(或集合).js and that should be rendered on the page. These objects are called “virtual nodes” or VNode
s for short.
虚拟DOM的主要目的是实现更快、更有效的DOM操作. When there are lots of nodes in the DOM, 就处理能力和所需资源而言,更新它们可能代价高昂.
Working with the virtual DOM JavaScript object is significantly faster. Subsequently, Vue.js organizes DOM updates in batches for more efficiency.
Consider the following code (index.html
is omitted for brevity.) What is it going to output in the browser? Please mention any notable console output as well.
const MockComponent = {
props: {
showMe: {
type: Boolean,
required: true,
},
},
template: `
This is a test component
`,
};
new Vue({
el: '#app',
components: {
MockComponent,
},
template: `
`,
});
A Boolean
prop works as boolean attribute in HTML. If a boolean prop is present and has no value (
), or is equal to the empty string (as in this example) or its own name in kebab case, its value is true. Therefore this example will output This is a test component
in the browser, and no error messages in the console.
Bonus points for mentioning that required
doesn’t make much sense here, as omitting this prop (i.e.
) is the proper way of setting its value to false.
Consider the following code (index.html
is omitted for brevity). What is it going to output in the console?
const MockComponent = {
render() {
return this.$slots.default;
},
data() {
return {
status: '',
};
},
watch: {
status: {
handler(newVal) {
console.log('Status update: ' + newVal);
},
immediate: true,
},
},
beforeCreate() {
this.status = 'initializing';
},
mounted() {
this.status = 'online';
},
beforeDestroy() {
this.status = 'offline';
},
};
new Vue({
el: '#app',
components: {
MockComponent,
},
template: `
`,
data() {
return {
showComponent: false,
};
},
mounted() {
this.showComponent = true;
window.setTimeout(() => {
this.showComponent = false;
}, 1000);
},
});
It will output:
Status update:
Status update: online
When the watcher runs immediately it uses the initial value, an empty string. The watcher isn’t triggered when beforeCreate
is fired, as reactivity hasn’t been initialized yet. Watchers are destroyed before beforeDestroy
is called.
当开发人员选择一个技术栈时,他们应该展示对替代解决方案的优缺点的理解. Vue.应该将js与最流行的前端框架React和Angular进行比较.
Performance
All three frameworks display similar performance. No significant difference here. However, 具有相同基本特性和功能的Angular应用比其他两个框架的编译大小更大.
Ease of Use
React is widely considered to have a steep learning curve. Developers need to learn JSX, ES6, and a build system (like webpack) before they can be productive with React. Create React App 假设你正在构建一个单页面应用程序,因此在其他场景中采用React将需要额外的学习时间.
Angular effectively requires knowing TypeScript to get started with it. Angular框架的设计目标是构建企业级应用,而且相当复杂. 在开始生产之前,开发人员需要熟悉广泛的框架API及其概念.
One design goal of Vue.js was for it to be incrementally adoptable. 这意味着你可以将其放入遗留项目中并开始使用它,而无需从头开始重建整个应用程序. Vue.js使用了web开发人员已经熟悉的核心web技术:HTML, CSS, and plain JavaScript (ES5). Using build tools is not required: You can include Vue with just a tag.
Suitability for Large-scale Apps
React and Angular were developed by Facebook and Google, respectively, so they have been enterprise-grade frameworks from the start. While Vue.js started as a one-man project, it’s come a long way since then, 吸引了大量的支持者和开发人员——其核心团队现在有20多名开发人员.
More to the point, Vue.js has been adopted by a number of companies including the likes of Adobe and Alibaba.
Thus, all three frameworks are well-suited for building large-scale apps.
Ecosystem
React is the most popular framework at the moment, having a considerably richer ecosystem than both Vue and Angular, with a large number of third-party libraries.
To sum up, Vue.js is on equal footing with other frameworks when building large-scale apps, 当您需要处理遗留应用程序或快速完成某些工作时,它非常出色. 但值得先检查一下生态系统对项目细节的支持.
There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.
Why Toptal
Tired of interviewing candidates? Not sure what to ask to get you a top hire?
Let Toptal find the best people for you.
Hire a Top Vue.js Developer NowOur Exclusive Network of Vue.js Developers
Looking to land a job as a Vue.js Developer?
Let Toptal find the right job for you.
Apply as a Vue.js DeveloperJob Opportunities From Our Network
Submit an interview question
Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.
Looking for Vue.js Developers?
Looking for Vue.js Developers? Check out Toptal’s Vue.js developers.
Matthew Newman
Matthew在数据库管理和软件开发方面拥有超过15年的经验, with a strong focus on full-stack web applications. He specializes in Django and Vue.拥有在AWS上部署服务器和无服务器环境的专业知识. He also works with relational databases and large datasets.
Show MoreSergej Kurbanov
Sergej是一名全栈开发人员,拥有超过7年的可扩展构建经验, feature-rich applications, workflow automation, and AI integrations. 从在过去四年中指导200多名女学生成为初级React开发人员,到构建自己的ai驱动SaaS产品, Sergej的参与方法和保持领先的奉献精神使他成为任何项目或团队的宝贵资产.
Show MorePatryk Pawłowski
Patryk是一位经验丰富的全栈开发人员,擅长所有类型的现代JavaScript实现——从构建后端和api到构建像素完美的web和移动应用程序. 这要归功于他经营自己公司的经验和设计背景, he is a great facilitator between business and product teams. Patryk also enjoys speaking at conferences.
Show MoreToptal Connects the Top 3% of Freelance Talent All Over The World.
Join the Toptal community.