How many major components does vue have?
Vue has 4 major components: 1. Global components. Use the "app.component(...)" method to register global components. Global components can be used in any component template of the application. 2. Local components are components registered in the "components" option of a (parent) component. 3. Dynamic components refer to components with different names that are rendered according to the different binding values to the attribute is. 4. Asynchronous components do not render immediately when the page is loaded. Instead, they wait until some business logic is completed before executing the logic in the component and rendering it to the page.
The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.
Vue’s component is essentially an instance with predefined options. We use small, independent and usually reusable components, which are assembled layer by layer to finally form a complete page.
Components must be registered first so that the Vue application can recognize them. There are two types of component registrations:
- Global registration
- Local registration
Global component
(in the root component) use the methodapp.component('component-Name', {})
to register the global component, global registration The component can be used in the template of any component in the application. (Learning video sharing: vuejs introductory tutorial, Basic programming video)
The first parameter is the component name, and it is recommended to follow the W3C specification Custom component names in (to avoid conflicts with current and future HTML elements): the letters are all lowercase and must contain a hyphen . The second parameter is the component's configuration options.
const app = Vue.createApp(); app.component('my-component', { template: `<h1>Hello World!</h1>` }); const vm = app.mount('#app')
⚠️ Although global components can be conveniently used in various components (including their own internals), this may cause an increase in the size of the project when building it and a unnecessary increase in the amount of JavaScript downloaded by users.
Need to register global components before app.mount('#app')
Application is mounted to the DOM
components option of a component within a (parent) component.
are defined by a common JavaScript object , and the parameters they receive are the same as global components, but they can only be used in the parent component, called local components.
For each property in thecomponents object, its
property name is the name of the custom element, and its property value is the option object of this component.
const ComponentA = { /* ... */ } const ComponentB = { /* ... */ } const ComponentC = { /* ... */ }
// 然后在父组件的 `components` 选项中定义你想要使用的组件 const app = Vue.createApp({ components: { 'component-a': ComponentA, 'component-b': ComponentB } })
## will display the corresponding component with the same name.
Attribute
Can be:
- The options object of a component
- Sometimes in order to save the state of a dynamic component when switching, such as the value of an input box in a component, you can use the
.
Attribute
can also be used to solve the rule restrictions of HTML element nesting. It is applied to the native HTML tag. Its value is the component name, so that the native tag is actually rendered. The content is the component. Because for
- 字符串,例如
template: '...'
- 单文件组件
.vue
<script type="text/x-template">
, <ol>
, <table>
and <select>
There are strict restrictions on the direct child elements allowed to be placed inside these elements. If embedded in other elements, it will be regarded as invalid content and promoted to the outside, causing final rendering problems. But if we need to use components as direct child elements in these elements, we can use the attribute is
on the "legal" child elements to specify the actual content to be rendered. In this case, the attribute is
Used on native HTML elements, such as <tr>
Its value needs to be prefixed with
vue: to indicate that what is parsed is actually a Vue component<table>
<tr is="vue:blog-post-row"></tr>
</table>
异步组件
现在的大型网页往往需要异步获取不同的数据,Vue 有一个 defineAsyncComponent
方法定义异步组件,以优化应用的加载和用户体验。
异步组件在加载页面时并不立即渲染,而是要等带一些业务逻辑完成后,才会执行组件内的逻辑和渲染到页面上。
// 全局组件 app.component('async-example', Vue.defineAsyncComponent(() => { return new Promise((resolve, reject) => { resolve({ template: '<div>I am async!</div>' }) }) })) // 局部组件 import { createApp, defineAsyncComponent } from 'vue' createApp({ // ... components: { AsyncComponent: defineAsyncComponent(() => { return new Promise((resolve, reject) => { resolve({ template: '<div>I am async!</div>' }) }) }) } })
异步组件的注册和一般的同步组件类似,如果是注册全局组件,也是使用 app.component()
进行注册,不过第二个参数使用 Vue.defineAsyncComponent()
方法告诉 Vue 应用该组件是异步组件。
defineAsyncComponent()
方法的参数是一个匿名函数,而且函数是返回一个 Promise。在 Promise 内应该 resovlve({})
一个对象,其中包含了构建组件相关配置参数。只有当 Promise resolve
或 reject
才执行异步组件的处理。
The above is the detailed content of How many major components does vue have?. 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

AI Hentai Generator
Generate AI Hentai for free.

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



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.

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.

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.

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.

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.

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.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.