Table of Contents
The origin of modularity
使用Vue.extend 来创建全局的Vue组件
使用Vue.component 创建出来的组件
通过templat元素,在外部定义的组件结构,这个方式,有代码的智能提示和高亮
好用,不错
Home Web Front-end Front-end Q&A What is the difference between componentization and modularization in vue

What is the difference between componentization and modularization in vue

Dec 15, 2022 pm 12:54 PM
front end vue.js

The difference between componentization and modularization: Modularization is divided from the perspective of code logic; it facilitates code layered development and ensures that the functions of each functional module are consistent. Componentization is planning from the perspective of UI interface; componentization of the front end facilitates the reuse of UI components.

What is the difference between componentization and modularization in vue

The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.

The origin of modularity

In the early days of web development, js was used as a scripting language to do some simple form verification or animation implementation, with a relatively small amount of code. , just write it in the script tag

With the emergence of ajax asynchronous requests, the front-end and back-end separation have gradually formed. The client needs to complete more and more things, and the amount of code is also increasing. . In order to cope with the sharp increase in the amount of code, we usually organize the code in multiple js files for maintenance

But there will be some problems: such as the problem of global variables with the same name

Use function closure Packages can solve the problem of variable conflicts, but variables defined in other files cannot be used.

Modularization has two cores: export and import

Learn about es6 modular development Bar

Keyword export (export) import (import)

For example, encapsulate a module that sends ajax

What is the difference between componentization and modularization in vue

In some cases, a module contains a certain function. We do not want to name this function. Let the importer name it by himself.

You can use export default

1

2

3

4

5

6

7

export default address

//只能默认一个

 

import jjj from "./aaa.js"

//可以自己命名,不要加{}

 

//导入 import

Copy after login

Why use modularity? The benefits of using modularity

Of course, all functions of the page can be realized without using modular development, but it will increase the pressure on server requests and reduce the user experience. When the page needs When there is too much data, one request contains all the requested data. What if the user does not need it? Is such a request still effective? Of course it has no effect, so this kind of problem can be solved by using the idea of ​​modular development

Briefly talk about the benefits of using modular development

1. Clear organization and easy maintenance

2. All data will not be requested back at once, and the user experience is good

3. Modules are isolated from each other, but internal members can be exposed through specific interfaces, or they can rely on others Module

Comparison between componentization and modularization

Many people understand componentization and modularization as one, and of course they are similar in many places , let’s take a look at the similarities below!

1. Componentization is an independent and reusable code organization unit. The component system is one of the core features of Vue, which enables developers to build large applications using small, independent and often reusable components; [Related recommendations: vuejs introductory tutorial, web front-end

2. Component development can greatly improve application development efficiency, testability, reusability, etc.;

3. Component usage is classified into: page components, business components, and general components. ;

4. Vue components are based on configuration. The components we usually write are component configurations rather than components. The framework will subsequently generate its constructors. They are based on VueComponent and extend Vue;

5. Common componentization technologies in Vue include attribute prop, custom events, slots, etc., which are mainly used for component communication, expansion, etc.;

6. Reasonable division of components helps to improve application performance ;

7. Components should be highly cohesive and low coupling;

8. Follow the principle of one-way data flow.

The difference between componentization and modularization:

Modularization: It is divided from the perspective of code logic; it facilitates hierarchical development of code , ensuring that each functional module has a single function

Componentization: It is divided from the perspective of UI interface, and the componentization of the front end facilitates the reuse of UI components

Three ways to define global components

The first way

1

2

3

4

<div id="app">

       <!-- 如果要使用组件,直接把组件的名称,以HTML标签形式,引入到页面中即可 -->

   <mycom1></mycom1>

   </div>

Copy after login

1

2

3

4

5

6

7

<script>

        var com1 = Vue.extend({

        template:&#39;<h3 id="使用Vue-extend-nbsp-来创建全局的Vue组件">使用Vue.extend 来创建全局的Vue组件</h3>&#39;

        })

        //1,使用Vue.component(&#39;组件名&#39;,创建出来的组件模板对象)

        Vue.component(&#39;mycom1&#39;,com1)

 </script>

Copy after login

The second way

1

2

3

4

<script>

Vue.component(&#39;mycom2&#39;,{template:&#39;<div><h3 id="使用Vue-component-nbsp-创建出来的组件">使用Vue.component 创建出来的组件</h3>s<span>template</span></div>&#39;

        })

</script>

Copy after login

The third way Kind

1

2

3

4

5

6

<templat id="temp">

 <div>

   <h1 id="通过templat元素-在外部定义的组件结构-这个方式-有代码的智能提示和高亮">通过templat元素,在外部定义的组件结构,这个方式,有代码的智能提示和高亮</h1>

   <h4 id="好用-不错">好用,不错</h4>

  </div>

</templat>

Copy after login

1

2

3

4

5

<script>

 Vue.component(&#39;mycom2&#39;,{

            template:&#39;temp&#39;

        })

</script>

Copy after login

Note: No matter which way the component is created, the template content pointed to by the component's template attribute must have and can only have one and only one root element.

The third method is best to use in work project development

[Related recommendations: vuejs video tutorial]

The above is the detailed content of What is the difference between componentization and modularization in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

How to solve cross-domain issues? A brief analysis of common solutions How to solve cross-domain issues? A brief analysis of common solutions Apr 25, 2023 pm 07:57 PM

Cross-domain is a scenario often encountered in development, and it is also an issue often discussed in interviews. Mastering common cross-domain solutions and the principles behind them can not only improve our development efficiency, but also perform better in interviews.

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

How to use Go language for front-end development? How to use Go language for front-end development? Jun 10, 2023 pm 05:00 PM

With the development of Internet technology, front-end development has become increasingly important. Especially the popularity of mobile devices requires front-end development technology that is efficient, stable, safe and easy to maintain. As a rapidly developing programming language, Go language has been used by more and more developers. So, is it feasible to use Go language for front-end development? Next, this article will explain in detail how to use Go language for front-end development. Let’s first take a look at why Go language is used for front-end development. Many people think that Go language is a

C# development experience sharing: front-end and back-end collaborative development skills C# development experience sharing: front-end and back-end collaborative development skills Nov 23, 2023 am 10:13 AM

As a C# developer, our development work usually includes front-end and back-end development. As technology develops and the complexity of projects increases, the collaborative development of front-end and back-end has become more and more important and complex. This article will share some front-end and back-end collaborative development techniques to help C# developers complete development work more efficiently. After determining the interface specifications, collaborative development of the front-end and back-end is inseparable from the interaction of API interfaces. To ensure the smooth progress of front-end and back-end collaborative development, the most important thing is to define good interface specifications. Interface specification involves the name of the interface

Is Django front-end or back-end? check it out! Is Django front-end or back-end? check it out! Jan 19, 2024 am 08:37 AM

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

Can golang be used as front-end? Can golang be used as front-end? Jun 06, 2023 am 09:19 AM

Golang can be used as a front-end. Golang is a very versatile programming language that can be used to develop different types of applications, including front-end applications. By using Golang to write the front-end, you can get rid of a series of problems caused by languages ​​​​such as JavaScript. For example, problems such as poor type safety, low performance, and difficult to maintain code.

See all articles