Dynamic component
Vue.js provide a special element
<component>
used to dynamically loading a different component, usingis
properties can be provided to mount the components
Application scenarios:
Asynchronous component
In large applications, we may need to divide the application into smaller code blocks and load a module from the server only when needed.
- Project packaging :
npm run build
- Generate a project under the root
dist
foldercss
: All packaged style files in the current projectjs
: The current project in the package alljs
filesapp.js
Allsrc
results of the directory content packagingapp.js.map
: The mapping file of the above filechunk.js
: All third-party packaged fileschunk.js.map
: The mapping file of the above file
index.html
: The static page of the project
- Generate a project under the root
Question :
- Performance: The packaged project, after running, will load all the files in the current project in one go
- Impact: The first time the page is loaded, it will be very slow and the user experience will be poor.
- solution:
- Asynchronous components can be used to solve this problem
usage:
- Synchronization components :
- Import method:
import login from './login.vue'
- Features :
- When packaging, it will be packaged into app.js
- When requesting a page, it will be requested along with app.js
- Asynchronous component
- Import method :
const login = () => import('./login.vue')
- Features :
- When packaging, it will not be packaged into app.js, but will be separately packaged as a js file
- When the page is requested, it will be loaded only when the request arrives
Note :
- If you use a component in vue, its name is: asynchronous component
- If used in routing , its name is: routing lazy loading