10 Essential Vue.js Interview Questions *
Toptal来源的基本问题,最好的Vue.Js开发人员和工程师可以回答这个问题. Driven from our community, we encourage experts to submit questions and offer feedback.
Hire a Top Vue.js Developer NowInterview Questions
A slot is a placeholder in a child component that is filled with content passed from the parent. Content of a regular slot is compiled in the parent’s scope and then passed to the child component.
因此,你不能在slot的内容中使用子组件属性. But scoped slots allow you to pass child component data to the parent scope and then use that data in slot content.
在Vue实例的data选项中定义的所有属性都是 reactive, meaning that if they change, the component is automatically updated and re-rendered as needed.
All such properties are converted to getters and setters during initialization, 从而允许Vue检测这些属性何时被访问或更改.
在设计Vue应用程序时,必须考虑以下限制:
- Vue cannot detect object property addition or deletion due to a JavaScript limitation, so the
Vue.set
方法添加新的根级反应性属性时必须使用. - 类似地,Vue无法检测何时使用索引修改数组项.
Vue.set
must be used here as well.
Mixin support is a feature that allows code reuse between components in a Vue.Js应用程序和软件组合工具.
A mixin is a JavaScript object that can contain any option that a component can contain. All mixin content is merged with a component’s options when that component uses a mixin.
Mixins有助于遵循DRY(不要重复自己)原则. mixin甚至可以全局地应用于每个组件实例. 在这种情况下,它被称为全局mixin.
Mixins是一个强大的工具,但在使用它们时需要谨慎. As with all injected code, we should be careful to avoid maintenance issues and unexpected behavior.
It helps to implement mixins using pure functions that don’t modify anything outside their own scope.
Global mixins should be avoided, as affecting every single component can lead to maintenance issues as an application grows. Injecting specific mixins to components as needed leads to more maintainable code.
申请加入Toptal的发展网络
并享受可靠、稳定、远程 Freelance Vue.js Developer Jobs
单文件组件是一个带有 .vue
包含Vue组件的扩展. It contains the component’s template, logic, and styles all bundled together in one file. It consists of one block, optional
and
块,以及可能的其他自定义块.
To use one, you need to set up Vue Loader for parsing the file (usually done as part of a webpack building pipeline). But this then also supports using non-default languages such as Sass or HTML templating languages with pluggable pre-processors.
Vue.js uses what’s called a one-way data flow. Data is passed to child components from a given parent component using a prop or a custom attribute that becomes a property on the child component instance.
When the parent component updates a prop value, it’s automatically updated in the child component. 不应该改变子组件中的属性. Also, it does not affect the parent component (unless it is an object or array).
子组件可以通过事件与父组件通信. The parent can assign a handler to any event emitted by the child component instance and data can be passed back to the parent. The child component can emit a special event for updating the props passed to it.
Memory leaks in Vue.js applications often come from using third-party libraries that create their own instances and/or manipulate the DOM. The v-if
directive and the Vue Router destroy Vue component instances; however, 任何第三方库之后的清理都应该在 beforeDestroy()
lifecycle hook.
例如,假设我们使用一个虚构的库PowerGraph.js, inside our component. 它创建了一个图形实例,在页面上显示一些数据:
mounted() {
this.chart = new PowerGraph();
}
我们需要调用图形实例 destroy()
方法,或者实现我们自己的清理方法:
beforeDestroy() {
this.chart.destroy();
}
如果在组件被销毁之前没有完成清理, 那么这些内存永远不会被释放. Hence, a memory leak.
The virtual DOM is a tree-like data structure (or a collection) of JavaScript objects representing DOM nodes that are managed by Vue.Js,并且应该在页面上呈现. 这些对象被称为“虚拟节点”或 VNode
s for short.
The main purpose of the virtual DOM is faster and more efficient DOM manipulation. 当DOM中有很多节点时, updating them can be expensive in terms of processing power and resources required.
使用虚拟DOM JavaScript对象要快得多. Subsequently, Vue.js分批组织DOM更新以提高效率.
Consider the following code (index.html
is omitted for brevity.)它将在浏览器中输出什么? 也请提及任何值得注意的控制台输出.
const MockComponent = {
props: {
showMe: {
type: Boolean,
required: true,
},
},
template: `
This is a test component
`,
};
new Vue({
el: '#app',
components: {
MockComponent,
},
template: `
`,
});
A Boolean
prop在HTML中作为布尔属性工作. 如果存在布尔属性并且没有值(
),或者等于空字符串(如本例中所示)或其本身的名称 kebab case, its value is true. 因此,这个示例将输出 This is a test component
在浏览器中,并且在控制台中没有错误消息.
Bonus points for mentioning that required
这里没有多大意义,因为省略了这个道具(i).e.
)是将其值设置为false的正确方法.
Consider the following code (index.html
is omitted for brevity). 它将在控制台中输出什么?
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
当监视器立即运行时,它使用初始值,一个空字符串. The watcher isn’t triggered when beforeCreate
被触发,因为反应性尚未初始化. Watchers are destroyed before beforeDestroy
is called.
When a developer chooses a tech stack they should display an understanding of the pros and cons of alternative solutions. Vue.js should be compared with the most popular front-end frameworks, React and Angular.
Performance
这三个框架都显示出类似的性能. No significant difference here. However, Angular apps with the same base features and functionality have a larger compiled size than two other frameworks.
Ease of Use
人们普遍认为React有一个陡峭的学习曲线. Developers need to learn JSX, ES6, 和一个构建系统(比如webpack),然后才能使用React. Create React App assumes that you are building a single-page app thus adopting React for other scenarios would require extra learning time.
Angular实际上需要了解TypeScript才能开始使用它. Angular framework design targets building enterprise-scale applications and is quite complex. Developers need to familiarize themselves with an extensive framework API and its concepts before starting to be productive.
One design goal of Vue.js was for it to be incrementally adoptable. This means you can drop it into legacy projects and start using it without rebuilding the whole app from the ground up. Vue.js uses core web technologies that web developers are already familiar with: HTML, CSS, and plain JavaScript (ES5). 不需要使用构建工具:您可以使用 tag.
Suitability for Large-scale Apps
React和Angular是由Facebook和Google开发的, respectively, 所以它们从一开始就是企业级框架. While Vue.js started as a one-man project, it’s come a long way since then, attracting a large number of supporters and developers—its core team now has more than 20 developers.
More to the point, Vue.js has been adopted by a number of companies 包括Adobe和阿里巴巴等公司.
因此,这三个框架都非常适合构建大型应用程序.
Ecosystem
React是目前最流行的框架, 拥有比Vue和Angular更丰富的生态系统, 具有大量的第三方库.
To sum up, Vue.在构建大型应用程序时,Js与其他框架处于同等地位, and it excels when you need to work with legacy applications or jump in and get something done fast. But it’s worth checking ecosystem support for your project’s particulars first.
面试不仅仅是棘手的技术问题, 所以这些只是作为一个指南. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, 回答所有问题也不能保证成为A级考生. At the end of the day, 招聘仍然是一门艺术,一门科学,需要大量的工作.
Why Toptal
Tired of interviewing candidates? 不知道该问什么才能让你得到一份好工作?
让Toptal为你找到最合适的人.
Hire a Top Vue.js Developer NowOur Exclusive Network of Vue.js Developers
Looking to land a job as a Vue.js Developer?
让Toptal为你找到合适的工作.
Apply as a Vue.js DeveloperJob Opportunities From Our Network
Submit an interview question
提交的问题和答案将被审查和编辑, 并可能会或可能不会选择张贴, 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
Freelance Vue.js Developer
Matthew has over 15 years of experience in database management and software development, 专注于全栈web应用程序. He specializes in Django and Vue.js with expertise deploying to both server and serverless environments on AWS. 他还处理关系数据库和大型数据集.
Show MoreSergej Kurbanov
Freelance Vue.js Developer
Sergej is a full-stack developer with over seven years of experience building scalable, feature-rich applications, workflow automation, and AI integrations. From coaching 200+ female students in becoming junior React developers over the last four years to building his own AI-driven SaaS product, Sergej's engaging approach and dedication to staying ahead of the curve make him an invaluable asset to any project or team.
Show MorePatryk Pawłowski
Freelance Vue.js Developer
Patryk is a seasoned full-stack developer who specializes in all types of modern JavaScript implementations—from architecting the back end and APIs to building pixel-perfect web and mobile apps. Thanks to his experience running his own company and having a background in design, 他是业务团队和产品团队之间的伟大促进者. 帕特里克也喜欢在会议上发言.
Show MoreToptal Connects the Top 3% 世界各地的自由职业人才.
Join the Toptal community.