Home>Article>Web Front-end> [Compilation and summary] 20 high-frequency Vue interview questions
As a programmer, if you want to find your favorite job, you will inevitably ask a lot of eight-part essays. Although some of them have nothing to do with work, but you If you want to pass the interview, you must know it. So I recently started to summarize some interview questions, firstly to strengthen my understanding and find a good job, and secondly to help as many friends as possible to quickly master relevant knowledge or successfully pass the interview?
This article introduces some common problems with vue. I try to arrange these questions according to difficulty and relevance to make them easier to transition and understand. Of course, this article is not just as simple as an eight-legged essay, these knowledge points are often used in work. [Related recommendations:vuejs video tutorial]
This question is generally not asked much, but if it is asked, you must Got to answer
Answer
This is a little more complicated than the previous question. You can try to understand the memory or just remember it directly
Rendering process
Vue2(optional API) | Vue3(setup) | Description |
---|---|---|
- | Before instance creation | |
- | After the instance is created | |
onBeforeMount | Called before DOM is mounted | |
onMounted | DOM mounting completion call | |
onBeforeUpdate | Called before data update | |
onUpdated | Called after data update | |
onBeforeUnmount | Called before component destruction | |
onUnmounted | Component destruction Complete call |
它们的具体用法可以参考我的这篇文章盘点Vue2和Vue3的10种组件通信方式(值得收藏) - 掘金 (juejin.cn)
除了上面的传参方式你也可以回答Vuex和Pinia,前提你了解这两个状态管理器,因为你说了大概率下个问题就会问你Vuex和Pinia
面试问到这个问题的时候,不要上来就开始说什么state
,mutation
...。你要先阐述Vuex干嘛用的,什么时候需要用Vuex。
回答
Vuex是Vue中的全局状态管理框架,它可以管理应用的所有组件的状态。并不是每个项目都需要引入Vuex的,当我们的项目有很多个页面,并且这些页面共享着多个数据状态,此时我们可以引入Vuex。
Vuex有三个核心的概念,state
,mutations
,actions
,其中state
为存放数据的地方,mutations
中的函数作用则是用来修改state
,actions
中一般是用了处理一些异步操作的函数。
Vuex除了上面三个概念还有getters
,moudles
,getters
就像Vue中的计算属性computed
一样用来描述依赖响应式状态state中的复杂逻辑。moudles
则是可以将store分割成模块(module),每个模块都拥有自己的state
,mutations
,actions
等,在大型应用中经常用到
场景:当我们异步获取结果并赋值给state的时候,比如数据请求,我们可以在actions
中进行数据请求,拿到结果通过它的dispatch
方法调用mutations
中修改state
的函数,从而将结果赋值给了state
这个现在问的好像不多,从我最近面试来看只有我提到了才会问一下,但是以后问的肯定会越来越多。关于pinia问的一般是它和Vuex的区别,确切的说应该是它和Vuex4之间的区别
回答
pinia
其实就是Vuex5,它和Vuex的主要区别有以下几点
Pinia使用更简单,更符合开发者的开发习惯
pinia
中没有了mutations
,状态state
的修改可以直接进行修改,或者在actions
中修改,或者使用它的$patch
方法进行修改
pinia
中没有了modules
,如果想使用多个store,直接使用defineStore
定义多个store传入不同的id即可
更好的TS支持,不需要创建自定义的复杂包装器来支持TS
vue 官方提供了v-text、v-for、v-model、v-if 等常用的指令。除此之外vue 还允许开发者自定义指令。面试经常会问什么是自定义指令?你用自定义指令做过哪些功能?
回答1:什么是自定义指令?
自定义指令包含局部指令和全局指令,在模板中使用指令前必须先使用directives
选项注册。局部指令指在某个组件中注册,而全局则是将指令注册到全局,通常在main.js中注册。
自定义指令由一个包含类似组件生命周期钩子的对象来定义。它的生命周期钩子包含created
,beforeMount
,mounted
,beforeUpdate
,updated
,beforeUnmount
,unmounted
,
常用的钩子为mounted
和updated
,它接受el
,binding
等参数.binding
参数的值一般包含绑定到这个元素上的信息,比如下面这个指令
它的binding会是这个对象
{ arg: 'foo', modifiers: { bar: true }, value: /* `baz` 的值 */, oldValue: /* 上一次更新时 `baz` 的值 */ }
回答2:你用自定义指令做过哪些功能?
数据埋点,通过绑定自定义事件传入点击当前元素需要埋点的事件名,在指令中监听当前元素的点击事件后调用后台接口将事件名传入
权限控制,通过绑定自定义事件传入控制当前元素的权限字段,在指令中获取到当前元素根据权限字段来控制该元素的状态(显示,隐藏等)
...
keep-alive
官网描述
is a built-in component whose function is to cache removed component instances when dynamically switching between multiple components.
Answer
Usually when we switch components, the previous component will be destroyed, and when we use
If you wrap it, the component will be cached, and the previous state will be retained when the component is displayed again.
keep-alive
Receives two attributesinclude
andexclude
, which respectively represent which components need to be cached and which No caching is required. It receives the component name array, string or regular expression. It can be used when we use dynamic componentscomponent
or routesrouter-view
keep-alive
also receives themax
attribute indicating the maximum number of cache instances. If this number is exceeded, the cache instance that has not been accessed for the longest time will be destroyed.
keep-alive
has two life cycles, namelyactivated
anddeactivated
,activated
The hook will be called when it is mounted for the first time or every time it is reinserted from the cache.deactivated
The hook is called when the component is removed from the DOM or the component is uninstalledvue2 Mixin-Mixin
There is no longer the concept of
Mixin
in vue3, so the chance of being asked will become smaller and smaller in the future, but the frequency of being asked is still very high. Generally speaking, you will learn about its concept, advantages and disadvantages, and sometimes you will also ask about the life cycle execution order of it and the parent componentvue official website description:
Mixin provides a very Flexible way to distribute reusable functionality in Vue components. A mixin can contain arbitrary component options. When a component uses a mixin, all of the mixin's options will be "mixed" into the options of the component itself.
Answer1.
Mixin
's function is to extract the public logic of the component. When which component needs to be used, directly mix the extracted part into Just inside the component 2. The life cycle ofMixin
will be executed before the life cycle of the parent component. If the properties or methods inMixin
conflict with the parent component, the properties or methods in the parent component will be used. 2. Advantages: It can reduce code redundancy and improve logic reusability. 3. Disadvantages: Naming conflicts are easy, it is difficult to trace the source, and later troubleshooting is inconvenientvue responsiveness principle
This question is asked very frequently, so This question has been asked in almost all the interviews I have experienced, and it is an opening question in Vue.
The following is my own understanding answer, taking vue2 as an example, you can refer to it
- ##The responsive principle of vue is based on
Object.defineProperty
This API is used to hijack data and implement it in conjunction with the publisher-subscriber model.
- will first use
After hijacking each attribute, multiple subscribers will be bound to this attributeget## in
Object.defineProperty
#Function to access and hijack all attributes of data in vue, which involves hijacking deeper attributes in data and requires recursively calling the hijacking method. This is implemented through aObserver
class- watcher
, Because an attribute may be used in many places; and this
watcher
contains the functionupdate
that updates the view. The corresponding relationship between- watcher
Multiple subscribers of each attributeand the attribute and the connection with the view are realized by compiling the template
Compile
class.Compile
will get the entire dom object, and then traverse the element child nodes to obtain the data attribute in vue, then directly add awatcher
to the attribute and give it some updates to the current view. Method.- watcher
setwill be added to the corresponding array, here it is through the
When a property in data changes, theDeps
class Implemented, when initializingwatcher
, theaddSub
method inDeps
will be called to add theSubs# of this class corresponding to
watcher##In the array
- function in
The difference between Object.defineProperty and proxyObject.defineProperty
will be triggered. This The
notifyfunction in the
Depsclass of the property will be called to traverse the subscribers
watcherin the
Subsarray and call its function
updateTo trigger the update of the view
Generally, you may ask after asking about the principle of responsiveness The difference between the two
Answer
can only proxy properties,
- Proxy
can monitor it, butThe proxy is the object.
ProxyNew properties are added to the object,
- Object.defineProperty
cannot.
Object.defineProperty
的代理行为是在破坏原对象的基础上实现的,Proxy 则不会破坏原对象,只是在原对象上覆盖了一层。数组新增修改,
Proxy
可以监听到,Object.defineProperty
不能。
Proxy
不兼容IE11
及以下vue3内置组件
vue3新增了两个内置组件分别是
Teleport
和Suspense
。
Teleport
组件可以称之为传送门,作用将其插槽内容渲染到 DOM 中的另一个位置,接收两个参数to(要去的位置)和disabled(是否留在原位置)。接收比如下面代码
video将会被传送到id为popup的元素下。
Suspense
组件
组件用于协调对组件树中嵌套的异步依赖的处理。
它一般用于包裹多个异步组件处理多个异步组件加载前与完成后的统一状态
组件有两个插槽:
#default
和#fallback
,在初始渲染时,将在内存中渲染其默认的插槽内容。如果在这个过程中遇到任何异步依赖,则会进入挂起状态等待异步组件加载完毕。在挂起状态期间,展示的是
#fallback
插槽内容nextTick及原理
关于nextTick会问到它的用法,然后是它的原理,然后还可能问到JS的时间循环机制。
问题1:vue中的nextTick是干什么用的?
这个其实比较简单,用过都知道它是干嘛的,vue官方的解释是:
在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。
这是什么意思呢,其实vue中修改data不会立刻触发dom更新;而是把需要更新的Watcher加入到queueWatcher队列中,然后在合适的时机在nextTick中调用这些Watcher的更新函数进行dom更新,所以在data刚被修改的时候,我们是获取不到更新后的dom的,这时候便需要调用nextTick函数在它的回调函数中获取到变化后的dom
问题2:nextTick原理
nextTick原理是借助浏览器事件循环来完成的,因为每次事件循环之间都有一次视图渲染,nextTick尽量在视图渲染之前完成dom更新,所以nextTick优先使用的是promise(微任务)实现
每次执行nextTick时会将传入的回调函数放入一个队列中(callbacks数组),然后当在本次事件循环的同步代码执行完毕后开启一个微任务(promise或者MutationObserver)去依次执行这个callbacks中的回调函数。
但是当浏览器不支持promise的时候在vue2中会进行进行降级处理,依次使用
setImmediate
、setTimeout
开启一个宏任务执行callbacks当一个data数据更新时对应的watcher便会调用一次nextTick,将它对应的dom更新操作作为回调函数放入callbacks中,所以当我们想获取这个data更新后的dom需要在其值变化后也调用nextTick将回调函数传入排在上个更新dom的回调函数后面,所以我们可以在这个nextTick的回调函数中获取到更新后的data
问题3:js事件循环机制
不属于vue,后面文章再介绍
vue虚拟dom,diff算法
这题在工作中有用吗是???答案是没有用,但是在面试中有用啊,所以我们要会回答?
问题1:什么是虚拟dom?
简单来说就是一个描述dom结构的js对象
问题2:为什么要用虚拟dom?
每当我们用原生JS或者JQ操作
DOM
时,浏览器会从头开始进行DOM
树的构建,频繁的操作DOM
开销是很大的。而虚拟
DOM
就是为了减少这些操作的,虚拟DOM
首先会通过状态生成一个虚拟节点树(js对象),然后使用虚拟节点树进行渲染。当某些状态发生变更时会生成新的虚拟DOM节点树,然后与上一次虚拟DOM节点树进行比较(diff),从而找到差异的部分,最后渲染到真实的DOM节点上面问题3:说一下diff算法
The essence of the diff algorithm is to find the difference between two objects, with the purpose of reusing nodes as much as possible. In vue, it is an algorithm used to calculate the differencebetween the virtual DOM after the change and the virtual DOM before the change when the state changes.
Let’s talk about Vue’s double-ended diff algorithm - Nuggets (juejin.cn)
Conclusion
web front-end development,Basic programming video)
Method Vue2 Vue3 Father to son props props From son to father $emit emits Father to Son $attrs attrs Son to Father $listeners None (merged into attrs mode) father-to-child provide/inject provide/inject Child component accesses parent component $parent None Parent component accesses child component $children None Parent component accesses child component $ref expose&ref ##Brother Component passing value ##Object.definePropertyThe specific implementation cannot be explained clearly in a few sentences. I recommend an articleThe above questions are basically summarized by consulting some information and combining with my own understanding, so it is inevitable that there will be some errors and inadequacies. If you find them, I hope you will feel free to point them out. Many thanks.(Learning video sharing:EventBus mitt
The above is the detailed content of [Compilation and summary] 20 high-frequency Vue interview questions. For more information, please follow other related articles on the PHP Chinese website!