1: Vue-router is used basically
-
The index.js file in the Router file
-
Import vue and vue-Router in the index.js router file
-
Register via vue.use (VueRouter)
-
Const router = new VueRouter({router})
-
Router = [{},{},{}]
- In the main.js file
- Import the route object exported from index.js in the router
- The Router object is registered when the Vue instance is created (registered in the Vue instance object because the injection Router and Router and router and Route property methods are created in the Vue instance)
- In the app.vue file
- Use router-view as a placeholder
Two: Dynamic routes
- Const ‘name’ = () => import(‘ path ‘)
- For example, (path :’./detail/:id’) obtains the route ID
- A: enclosing the route. The params. Id ∣ ∣ enclosing the route. The params. Id | | this. The route. The params. Id ∣ ∣ enclosing the route. The query. Id
- Method 2: After enabling props(props: True) during route configuration, the URL parameters are passed to the component, and the component accepts the URL parameters using props
Three: set routine by
- The same parts use the same code, and different parts use router-view for placeholders
- Child route: Children
Four: programmatic navigation
- this.$router
- Push records history
- Replace does not record history
- Go Jumps to the historical location
- By reference 1: this. $router. Push ({name: ‘path’ params: {id: 1}})
- This.$router. Push ({path:’ path ‘,query:{id:2}})
- The difference is whether the browser URL is visible
Five: Difference between Hash mode and History mode
- The difference of expression form
- Hash mode path with Hash sign,? A. Carries parameters after
- Is the History mode different from Hash with #?
- The principle difference between
- The Hash mode is based on the stroke point and onHashchange event
- The History mode is based on the History API in HTML5
- History. PushState (), and the history. ReplaceState ()
Six: History mode
- The History mode requires server support
- Applications in a single page, the server does not exist http:www.texturl.com/login this address…
- The server should return single-page application index.html except for static resources
- Need to add (mode:’history’) when creating VueRouter
- Vue-cli Server has configured the history mode
Seven: Implementation principle of VueRouter
-
Front knowledge
-
The plug-in
-
Mixed with
-
Vue.observable
-
slot
-
Render function
-
Run time and full Vue
-
Hash mode (location.url)(# after the address to send changes to change the browser does not send requests to the server, it will log the history to the browser)
-
The content after # in the URL is the path address
-
Listen for the hashChange event
-
Find the corresponding component according to the current routing address and re-render
-
History mode (the browser does not send requests to the server)
-
Change the address bar with the history.pushstate () method
-
Listen for popState events (triggered by clicking browser back or Forward and Go)
-
Find the corresponding component according to the current routing address and re-render
VueRouter simulation implementation – analysis
- Use can pass in functions or objects. If a function is passed in, vue. use will call the function directly, or if an object is passed in, vue. use will call the install method of that object internally
-
The first box is the name
-
– The second box is properties
-
The third box is method _ for static methods
Nine: VueRouter – install
-
Use CLI scaffolding to create a project and lead routing
-
Create a vuerouter folder storage module under the SRC directory
-
Create the index.js file in the vuerouter folder
Let _Vue = null // Export default class VueRouter {// Define a static install method, pass the Vue constructor static install(Vue){//1. Check whether //installed is an added attribute to the current creation. True representatives have installed the if (VueRouter. Install. Installed) {return} VueRouter. The installed = true / / 2. Log the Vue constructor to the global variable (because vuerouter’s instance method uses the Vue constructor) _Vue = Vue //3. Mixin ({beforeCreate(){if(this.options.router){ _Vue.prototype.router = this.$options.router } } }) } }
VueRouter- constructor
Constructor (options){// initialize 3 values // Record the options passed to this object. Options = options RouterMap = {} // This. RouterMap = {} // This is a responsive object (observable provided in Vue converts it to a responsive object). This.data = _vue.Observable ({// stores the current route, value is default)} this.data = _vue.Observable ({// stores the current route, value is default)}Copy the code
11: VueRouter – createRouterMap
-
The router is converted from the option passed by the constructor into a key-value stored in the routerMap of the constructor
CreateRouterMap (){// Traverses all the rules, The routing rules parsed into the form of key-value pairs stored in routerMap object / / routes (this is the array to store the routing address) this. Options. Routes. ForEach (route = > {enclosing routerMap route. The path = route.component }) }
Twelve: VueRouter – the router – the link
-
Error reported at runtime because the Vue runtime version does not support the template template
-
This can be done using the full Vue or render function
Home About
// Take a Vue constructor as parameter initComponents(Vue){// Create a component with Vue // name, Vue.component(‘router-link’,{props:{to:String}, template:’})}
Init (){this.createrouterMap () this.initComponents(_Vue) this.initEvent()} _Vue. Mixin ({this.createrouterMap () this.initComponents(_Vue) this.initEvent()} _Vue. Prototype.$router = this.$options.router this.$options.router.init() } } })Copy the code
VueRouter- Full version of Vue
-
Run time edition: Does not support template templates, need to package whether to compile ahead of time
-
Full version: contains the runtime and compiler, the volume of the runtime version is about 10K, the program runs when the template into render function
-
Create the vue.config.js file
-
module.exports = { runtimeCompiler:true }
module.exports = { runtimeCompiler : true }
VueRouter- Full version of render
InitComponents (Vue){Vue.component('router-link',{// Props :{to: String, }, // The first argument is the selector for the element we want to create (such as the tag selector) // The second argument sets some elements for the tag Return h("a",{attrs sets the attribute attrss:{href:this.to}, //router-view on:{// register the event click: This. ClickGandler}}, // This.$slots.default])}, //router-view to prevent click events methods: {clickGandler(e){// The first argument is data, the second argument is stitle, and the third argument is url history.pushState({}, ",this.to) // Load the corresponding component (let's re-render the view below) this.$router.data.current = this.to e.preventDafault() } } }) }Copy the code
15: VueRouter – the router – the view
InitComponents (Vue){const self = this Vue.component("router-view",{render(h){render(h){// Render current route component const component = Self.routermap [self.data.current] // Use the h function to convert the component to the virtual DOM return h(Component)}})}Copy the code
16: VueRouter – initEvent
-
Fixed an issue where the browser’s forward and back buttons clicked on the address bar to send changes but the view did not send changes
-
Call popstate
InitEvevt (){window.adDeventListener (‘ popState ‘,() => {this.data.current = window.location.pathName})}