I haven’t summarized Vue related knowledge before. I read a lot of others’ summaries, and in fact, I can know some quickly. However, when I met the real interview, I found that my knowledge was still fragmented, so I think I can’t, and I need to summarize it.
I don’t know if you are already preparing for the spring recruitment interview, how are you preparing? How is Vue reviewing for the interview?
If you feel in vUE this area is still relatively weak, it is better to do a mock interview, see everyone can not score full marks, wish you good luck, đ¸ answer for reference đ¸
Welcome to the GitHub repository, there are already 552 DCC questions, covering all kinds of front-end questions
—— Get to the point and become the interviewer ——
đđ on the phone, cough hello, can you hear me, can you hear me đ, the interview begins, why don’t you introduce yourself first? When you introduce yourself, I just look at your projects, tech stacks.
First question, just to get to the bottom:
Do you know (use) React or Angular? What’s the difference between them?
The answer
Vue borrows from Angular’s template and data binding technologies and React’s componentization and virtual DOM technologies.
đļ more familiar with Vue is it ~ (here only say Vue suppose you only skilled Vue)
How about your understanding of Vue first?
The answer
Website introduction: cn.vuejs.org/index.html
Key points: progressive JavaScript framework, core libraries plus plug-ins, dynamic user interface creation (asynchronous acquisition of background data, data display in the interface)
Features: MVVM mode; Code compact, small volume, high efficiency, suitable for mobile PC terminal development; It only focuses on the UI (similar to React) and can easily be developed by introducing Vue plug-ins or other third-party libraries.
đ¸ Think about the points you’ve made. Are they clear to you?
Based on your understanding of Vue, I will continue to talk about:
You just mentioned MVVM, can you elaborate on that?
The answer
Full name: model-view-viewModel, Model represents the data Model layer. The ViewModel is the bridge between the View and Model layers. Data is bound to the ViewModel layer and automatically rendered to the page. The ViewModel layer is notified to update data when the view changes.
đļ touch the bottom almost, asked the foundation, response type data to know, ask
How does VUE implement responsive data? (Responsive data principle) â
The answer
Vue2: Object.defineProperty redefines all attributes in data. Object. DefineProperty can make data acquisition and setting add a intercepting function, intercepting the acquisition of attributes, and collecting dependencies. Intercepts updates to properties for notification.
Specific process: First Vue uses initData to initialize the parameters passed in by the user. Then it uses a new Observer to observe the data. If the data is of an object type, it calls this.walk (value) to process the object. DefineeReactive loop Object properties are used internally to define responsive changes, and the core is to redefine the data using object.defineProperty.
đ¸ If you said object detection and then didn’t say array processing, I would ask the following question
So how does vue detect array changes?
The answer
An array is defined by using object.defineProperty. We all know how to change an array: pop, push, shift, unshift, splice, sort, reverse. As soon as these methods execute and change the contents of the array, I’m just going to update the contents, so that makes sense.
-
It’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack, it’s a function hijack.
-
Each item in the array may be an object, so I will observe each item in the array (and only the objects in the array can be observed, and the observed items will not be observed).
Vue3: Use proxy to directly listen for changes in the object array.
Then tell me about the event binding principle of Vue
The answer
-
Native DOM binding: Vue calls createElm when creating the real DOM, and invokeCreateHooks are called by default. It iterates through the relative property processing code under the current platform, such as the updateDOMListeners method, passing in the add () method internally
-
Component binding events, native events, custom events; Component bindings are implemented through the custom $on method in Vue.
(It can be understood as: The nativeOnOn of the component is equivalent to the ordinary element ON. The ON of the component will be processed separately.)
V-model in the implementation principle and how to customize v-Model â
The answer
The V-Model can be thought of as the syntactic sugar (component) of the Value + Input method. The native V-Model generates different events and attributes depending on the tag. Parse an instruction.
Custom: write the model property and put the prop and event inside it
đ is ok. If you know about reactive data and data binding, then ask about rendering.
Why does Vue use asynchronous rendering?
The answer
Vue is a component-level update. If asynchronous update is not adopted, the current component will be re-rendered every time the data is updated. Therefore, for the sake of performance, Vue will update the view asynchronously after this round of data update. Core idea nextTick.
Dep.notify () notifies watcher of the update, subs[I]. Update calls watcher’s update in turn, queueWatcher dequeues watcher, NextTick (flushSchedulerQueue) flusks the watcher queue in the nextTick (asynchronous).
đ¸ then ask, if you nextTick can speak very clearly that basically you understand.
Do you know nextTick?
The answer
The asynchronous method, the last step of asynchronous rendering, is closely related to the JS event loop. Using macro tasks and microtasks (setTimeout, promise, etc.), we define an asynchronous method. Multiple calls to nextTick will put the method in a queue and asynchronously clear the current queue.
Ok ok, ask you a life cycle first, I think again how to stump you đ
Talk about Vue’s life cycle â
The answer
When is it called?
- BeforeCreate: Called after instance initialization and before data observation
- Created: called after the instance is created. Instance completion: data observation, operation of attributes and methods,
watch/event
Event callback. There is no$el
. - BeforeMount: called before mounting, related
render
Function is called for the first time - Mounted: Was newly created
vm.$el
Replace, and call the change hook after mounting the instance. - BeforeUpdate: Called before data update, which occurs after the virtual DOM is rerendered and patched, and after which the change hook is called.
- Updated: The virtual DOM is rerendered and patched due to a data change, after which the change hook is called.
- BeforeDestroy: Called before the instance is destroyed, the instance is still available.
- All event listeners and all child instances will be removed from the Vue instance. You will see that the Vue instance is destroyed and all the event listeners will be removed from the Vue instance
What can be done within each lifecycle?
- Created: The instance has been created and can be used to request data or resources because it is the first to be triggered.
- Mounted: The instance has been mounted and some DOM operations can be performed.
- BeforeUpdate: You can further change the state in this hook without triggering rerendering.
- Updated: Performs DOM dependent operations, but avoid changing the state, which may result in an update wireless loop.
- Destroyed: You can perform some optimization operations to clear the timer and unbind the event.
In which lifecycle is Ajax placed? : is usually placed in Mounted to ensure logical unity because the life cycle is executed synchronously and Ajax is executed asynchronously. Singular server rendering SSR is also created because server rendering does not support mounted methods. When to use beforeDestroy? : The current page uses $on and needs to unbind the event. Know the timer. Unbind the event, scroll mousemove.
Parent-child component lifecycle call order (simple)
The answer
Render order: father before son; finish order: son before father
Update order: The parent updates the child, and the child updates the parent
Destruction order: father before son; completion order: son before father
Vue component communication â
The answer
-
Communication between father and son: Father provides data to son using the attribute props; The son binds his father’s event via $on and triggers his own event via $EMIT (publish subscribe)
-
$parent, $children,
Method to get parent and child component instances.
-
The parent component provides the data and the child component injects it. Provide, inject, and use more plugins.
-
Ref gets the component instance and calls the properties and methods of the component
-
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
Vuex state management to achieve communication
How Vuex works
The answer
Liverpoolfc.tv: vuex.vuejs.org/zh/
- Vuex is a state management pattern developed specifically for vue.js applications.
- The state self-management application consists of the following parts:
- State, the data source driving the application;
- View, which maps state to the view declaratively;
- Actions, which responds to state changes caused by user input on the view. The following is a schematic diagram of unidirectional data flow:
- Vuex, multiple components share state, because – the simplicity of unidirectional data flow is easily broken:
- Multiple views depend on the same state.
- Behaviors from different views need to change the same state.
Ask the virtual DOM, see if you can explain the transition from real DOM to virtual DOM, and tell me about Diff
How do I go from the real DOM to the virtual DOM
The answer
Related to the template compilation principle in Vue, the main process:
-
Convert the template to an AST tree. The AST uses objects to describe the real JS syntax (convert real DOM to virtual DOM).
-
Optimization of tree
-
Generate code from the AST tree
Use VNode to describe a DOM structure
The answer
A virtual node is an object that describes a real DOM element. The template (real DOM) is first converted to an AST. The AST tree generates the render function through CodeGen. The _c method in the render function converts it to a virtual DOM
The diff algorithm
The answer
Time complexity: the complete DIFF algorithm of a tree is a time complexity of O(n*3), which is optimized by VUE into O(n).
To understand:
-
Minimum number of updates, key is important. This can be a unique identifier for the node, telling diff that they are the same DOM node before and after the change
- extension
v-for
Why should there bekey
, there is nokey
I’m going to use it violently. I’m going to give you an example like moving a node or adding a node (modifying the DOM), pluskey
It only moves the DOM to reduce manipulation.
- extension
-
Only the same virtual node will be compared, otherwise the old one will be forcibly deleted and the new one will be inserted.
-
Only comparisons are made on the same layer, not across layers.
Diff algorithm optimization strategy: four hit search, four Pointers
-
Old before and new before (in the case of inserting and deleting nodes)
-
Old after and new after (compared to the end, before insert or delete case)
-
Old before and new after (head to tail ratio, this happens, involves moving nodes, so the nodes that the new forward points to, move to the old after)
-
Old back and new front (tail to head ratio, this happens, involves moving the node, so the node that the new front points to, moves to before the old front)
— After asking these questions, if it is clear, the basic O
Following these simple concepts, you will certainly be no problem đ
The Computed watch and method
The answer
Computed: The default computed is also a watcher that has a cache. The watcher computes the data when the dependent data changes, and reads the cached data when the data does not change. If one data depends on other data, use computed
Watch: You need to execute the function every time. Watch is more suitable for asynchronous operations when data changes. If you need to do something when some data changes, use Watch.
Method: Once the method is applied to the template, the view will be rerendered every time it changes, resulting in high performance overhead
The difference between v-if and V-show
The answer
-
V-if Will not render the DOM element of the node where the current directive resides if the condition is not valid
-
V-show simply toggles the current DOM display and hide
Why can’t v-for and V-if be used together
The answer
V-for has a higher priority than V-IF, which can cause performance problems by adding every element of V-IF.
What problems will V-HTML cause (simple)
The answer
-
XSS attacks
-
V-html replaces the elements inside the tag
Describes the component rendering and update process
The answer
When a component is rendered, the constructors of the child components are built and instantiated through the vue.extend() method. Finally, mount is done manually by calling $mount(). The patchVnode process is implemented when components are updated. The core of the process is the DIff algorithm.
Why is data in a component a function
The answer
Avoid data interaction in components. Multiple instances of the same component being reused will be created, using the same constructor if data is an object. In order to keep the components’ data independent, each component must return an object as the component’s state through the data function.
Why use asynchronous components?
The answer
- To save the result of packaging, asynchronous components are packaged separately, and jSONP is used for loading, effectively solving the problem of oversized files.
- The core is that the package component definition becomes a function, a dependency
Import ()
Syntax, you can achieve the split file loading.
See the official document in detail: cn.vuejs.org/v2/guide/co…
Difference between action and mutation
The answer
mutation
It’s a synchronous update,$watch
Errors are reported in strict modeaction
Is an asynchronous operation that can be invoked after obtaining datamutation
Submit final data
The difference between slots and scoped slots
slot
The answer
-
When a component virtual node is created, the child virtual nodes of the component are saved. When initializing components, the sons are classified by slot properties {a:[vnode],b[vnode]}
-
The rendering component replaces the node with the corresponding slot attribute. (Slots are scoped to parent components)
Scope slot
The answer
-
Scope slots are not parsed as child nodes of components. Is parsed into a function that is called for rendering when the child component is rendered.
-
The scope of a normal slot rendering is the parent component, and the scope slot’s rendering scope is the current child component.
How to extract the same logic from VUE
The answer
In fact, it is a matter of looking at the use of vue.mixin and mixing some common logic into functions for each lifecycle of a component.
Talk about keep-alive
The answer
Keep-alive caches components without uninstalling them when they are switched. Include /exclude, that is, activated and deactivated
Vue performance optimization
The answer
Coding optimization:
- The event agent
keep-alive
- Break up the component
key
Guaranteed uniqueness- Lazy route loading, asynchronous component
- If the throttle
Optimized Vue loading performance
- Import third-party modules on demand (
babel-plugin-component
īŧ - Lazy image loading
The user experience
app-skeleton
Skeleton screenshellap
P shellpwa
SEO optimization
- pre-rendered
The last word
đđ if you think it’s a good idea, you can follow me at âī¸. We will keep updating in the future
âī¸âī¸ Finally, I wish all of you who are preparing the autumn recruitment and spring recruitment interview smoothly ~, harvest offer, let’s go đ¤! And happy New Year â¤ī¸ â¤ī¸ ~