


[Compilation and sharing] Some vue-router related interview questions (with answer analysis)
This article will summarize and share with you some vue-router related interview questions (with answer analysis), help you sort out the basic knowledge, and enhance the vue-router knowledge reserve. It is worth collecting, come and take a look!
The principle of vue-router
In a single-page application, switching between different components needs to be implemented through front-end routing .
Front-end routing
1.key is the path, value is the component, used to display the page content
2. Working process: when the browser's path changes , the corresponding component will be displayed. vue-router
’s routing function: Map components to routes and then render them
Mainly
How to change the URL without causing the page to refresh
How to detect the URL change [Related recommendations: vuejs video tutorial, web front-end development 】
The hash pattern of the route
hash is the
## in the URL ## and the following part, the URL after
# will not be sent to the server,
so changing the hash part in the URL will not cause the page to refresh- Window can monitor
onhashchange
event changes. When the hash changes, read the content after
#, match routing rules based on the information, and change the page routing by changing
window.location.hash.
Three ways to change the URL
- Change the URL through the browser forward and backward
- Change the URL through tags
- Change the URL through window.location
Advantages
- Only the front-end needs to configure the routing table, no back-end participation is required
- Good compatibility, all browsers can support it
- Changes in the hash value will not send requests to the backend, it is completely front-end routing
The hash value needs to be preceded by #, which does not comply with the url specification and is not beautiful
routing history mode - using the new pushState() and replaceState()## in the history interface of H5's history API
-
The#html5 # Method, used to add and modify records in the browsing history, change the page path,
so that the URL jump will not reload the page.
popstate - event is similar to the
hashchange
event, but the popstate event is somewhat different:
Only when making thebrowser popState will be called only when behavior
is triggered. It will be triggered when the user clicks the browser's back button and forward button, or uses JavaScript to call the History.back(), History.forward(), and History.go() methods. Advantages
Conforms to URL address specifications, no # is required, and it is more beautiful to use
- Disadvantages
When the user
manually enters the address or refreshes the page, a url request- will be initiated, and the backend needs to configure the index.html page The user cannot match the static resource, otherwise a 404 error will occur.
- The compatibility is relatively poor, and the new pushState() and
- replaceState( are used in the HTML5 History object. )
method requires the support of a specific browser.
What is the difference between $route and $router? $router is an instance object of VueRouter, representing the only router object for the entire application. Contains routing jump methods, hook functions, etc. $route is the current routing object, which represents the routing rules of this component. Each route will have a route object, which is a local object.
Vue-router parameters are passed in several ways, what are the differences?
How to specify the jump route | If no parameters are passed | Can parameters be passed without the required value | ||
---|---|---|---|---|
Path/params parameter | You must use name to specify the route | params is part of the route , If placeholders are configured, If this route has params parameters, but this parameter is not passed during the jump, the jump will fail or the page will have no content. |
Placeholders that are not included in the delivery path will not be displayed on the address bar and will be lost upon refresh |
query parameters |
Path? key1=val1 & key2=val2.... |
1. You can use name to specify the route 2. You can use path to specify the route |
query It is a parameter spliced after the url. It doesn’t matter if it is not available. | query will not
The above is the detailed content of [Compilation and sharing] Some vue-router related interview questions (with answer analysis). For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.
