Web Front-end
Vue.js
10 vuejs interview questions about routing vue-router (including answer analysis)10 vuejs interview questions about routing vue-router (including answer analysis)
This article will introduce you to 10 vuejs interview questions about routing vue-router. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Routing vue-router interview questions
1. What is the mvvm framework?
mvvm is Model-View-ViewModel. The design principle of mvvm is based on mvc.
MVVM is the abbreviation of Model-View-ViewModel. Model represents the data model and is responsible for business logic and data. Encapsulation, View represents the UI component responsible for interface and display, ViewModel monitors changes in model data and controls view behavior, handles user interaction, and simply connects the View layer and the Model layer through two-way data binding. Under the MVVM architecture, View and Model are not directly connected, but interact through ViewModel. We only focus on business logic and do not need to manually operate the DOM, nor do we need to pay attention to the synchronization of View and Model. (Learning video sharing: vue video tutorial)
2. What is vue-router? What are the components?
- Vue Router is the official routing manager for Vue.js. It is deeply integrated with the core of Vue.js, making it easy to build single-page applications.
-
<router-link></router-link>and<router-view></router-view>and<keep-alive></keep-alive>
3. Active-class is an attribute of which component?
active-class is the router-link terminal attribute, used to switch the selected style. This style will be applied when the router-link label is clicked
4 . How to define dynamic routing of vue-router? How to get the passed value?
- The creation of dynamic routes mainly involves using the path attribute. Dynamic path parameters are used, starting with a colon, as follows:
{
path: '/details/:id'
name: 'Details'
components: Details
}Access to the details directory All files, such as details/a, details/b, etc., will be mapped to the Details component.
- When matching the route under /details, the parameter value will be set to this.$route.params, so dynamic parameters can be obtained through this attribute
console.log(this.$route.params.id)
5. What kinds of navigation hooks does vue-router have?
- Global front guard
const router = new VueRouter({})
router.beforeEach((to, from, next) = {
// to do somethings
})to:Route, represents the target to enter, it is a routing object.
from:Route, represents the route that is currently leaving, and is also a routing object
-
next:Function, a method that must be called, The specific execution effect depends on the parameters called by the next method
- next(): Enter the next hook in the pipeline. If all hooks are executed, the navigation status is confirmed (confirmed)
- next(false): The current navigation of the terminal. If the browser URL changes, the URL will be recharged to the address corresponding to the from route.
- next(’/’)||next({path:’/’}): Jump to a different address. Current navigation terminal, perform new navigation.
* The next method must be called, otherwise the hook function cannot be resolved
- Global post-hook
router.afterEach((to, from) = {
// to do somethings
})The post hook does not have a next function, nor does it change the navigation itself.
-
Route exclusive hook
- beforEnter
const router = new VueRouter({
routes: [
{
path: '/home',
component: Home,
beforeEnter: (to, from, next) = {
// to do somethings
// 参数与全局守卫参数一样
}
}
]
})- Intra-component navigation hook
const Home = {
template: `<div</div`,
beforeRouteEnter(to, from, next){
// 在渲染该组件的对应路由被 confirm 前调用
// 不能获取组件实例 ‘this’,因为当守卫执行前,组件实例还没被创建
},
beforeRouteUpdate(to, from, next){
// 在当前路由改变,但是该组件被复用时调用
// 例:对于一个动态参数的路径 /home/:id,在/home/1 和 /home/2 之间跳转的时候
// 由于会渲染同样的 Home 组件,因此组件实例会被复用,而这个钩子就会在这个情况下被调用。
// 可以访问组件实例 'this'
},
beforeRouteLeave(to, from, next){
// 导航离开该组件的对应路由时调用
// 可以访问组件实例 'this'
}
}- beforeRouterEnter cannot access this because the guard is called before the navigation is confirmed, so the new component has not yet been created. You can access the component instance by passing a callback to next. Execute the callback when the navigation is confirmed, and use the instance as the method parameter of the callback.
const Home = {
template: `<div</div`,
beforeRouteEnter(to, from, next){
next( vm = {
// 通过 'vm' 访问组件实例
})
}
}6. What is the difference between $route and $router?
- router is an instance of VueRouter. It is a global routing object that includes routing jump methods, hook functions, etc.
- route is a routing information object||jump routing object. Each route will have a route object, which is a local object and contains routing information such as path, params, hash, query, fullPath, matched, name, etc. parameter.
7. vue-router responds to changes in routing parameters
- Use watch to detect
// 监听当前路由发生变化的时候执行
watch: {
$route(to, from){
console.log(to.path)
// 对路由变化做出响应
}
}- Navigation hook function within the component
beforeRouteUpdate(to, from, next){
// to do somethings
}8. vue-router passing parameters
- Params
- Only name can be used, not Using the path
- parameter will not be displayed on the path
- The browser force refresh parameter will be cleared,
// 传递参数
this.$router.push({
name: Home,
params: {
number: 1 ,
code: '999'
}
})
// 接收参数
const p = this.$route.params-
Query:
- The parameters will be displayed on the path and the refresh will not be cleared
- name can use the path path
// 传递参数
this.$router.push({
name: Home,
query: {
number: 1 ,
code: '999'
}
})
// 接收参数
const q = this.$route.query
9. Two modes of vue-router
-
hash
- The principle is the onhashchage event, which can be monitored on the window object This event
window.onhashchange = function(event){
console.log(event.oldURL, event.newURL)
let hash = location.hash.slice(1)
}
-
history
- 利用了HTML5 History Interface 中新增的pushState()和replaceState()方法。
- 需要后台配置支持。如果刷新时,服务器没有响应响应的资源,会刷出404,
10. vue-router实现路由懒加载(动态加载路由)
- 把不同路由对应的组件分割成不同的代码块,然后当路由被访问时才加载对应的组件即为路由的懒加载,可以加快项目的加载速度,提高效率
const router = new VueRouter({
routes: [
{
path: '/home',
name: 'Home',
component:() = import('../views/home')
}
]
})
以上是经过参考很多同行分享与官方文档,汇总的一份总结,如有不对,请指出,最后感谢大家观看,求点赞,求分享,求评论,求打赏~~
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of 10 vuejs interview questions about routing vue-router (including answer analysis). For more information, please follow other related articles on the PHP Chinese website!
Netflix's Frontend: Examples and Applications of React (or Vue)Apr 16, 2025 am 12:08 AMNetflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.
The Frontend Landscape: How Netflix Approached its ChoicesApr 15, 2025 am 12:13 AMNetflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.
React vs. Vue: Which Framework Does Netflix Use?Apr 14, 2025 am 12:19 AMNetflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema
The Choice of Frameworks: What Drives Netflix's Decisions?Apr 13, 2025 am 12:05 AMNetflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.
React, Vue, and the Future of Netflix's FrontendApr 12, 2025 am 12:12 AMNetflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.
Vue.js in the Frontend: Real-World Applications and ExamplesApr 11, 2025 am 12:12 AMVue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.
Vue.js and React: Understanding the Key DifferencesApr 10, 2025 am 09:26 AMVue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.
Vue.js vs. React: Project-Specific ConsiderationsApr 09, 2025 am 12:01 AMVue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.






