search
HomeWeb Front-endVue.jsSummary of Vue basic knowledge: Vue component development

This article brings you about vue It mainly introduces issues related to vue component development. Component development provides an abstraction. We can develop an independent and reusable We use small components to construct our application components. Let’s take a look at them. I hope it will be helpful to everyone.

Summary of Vue basic knowledge: Vue component development

[Related recommendations: javascript video tutorial, web front-end

1. Functional programming

1. Introduction to functional programming

Functional programming is a programming method that treats computer operations as calculations of functions. The most important foundation of functional programming language is lambda calculus, and lambda calculus functions can accept functions as input (parameters) and output (return values).

Compared with imperative programming, functional programming emphasizes that the calculation of functions is more important than the execution of instructions.

Compared with procedural programming, function calculations in functional programming can be called at any time.

The filter function automatically filters all elements of the object, and returns true before it is stored in the specified object;

The Reduce function summarizes all elements inside the array;

2. Code examples

nbsp;html>


  <meta>
  <title>Title</title>


<div>
  {{totalPrice()}}
</div>
 
<script></script>
<script>
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;
    },
    methods :{
      totalPrice(){
        const nums = [10,9,21,16,7]
        let total = nums.filter(x => x<10).map(x => x*2).reduce((pre,n)=>pre+n);
        console.log(total)
        return total
      }
    }
  })
</script>

2. v-model

Form elements such as <input> and

1. v-model two-way binding

<input>

The implementation principle of v-model dynamic two-way binding essentially consists of two operations:

(1) v-bind binds a value attribute

(2) The v-on instruction binds the input event to the current element

nbsp;html>


  
  Title


     <input>   {{message}}
  <script></script> <script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;哪吒&#39; }, methods: { valueChange(event){ this.message = event.target.value; } } }) </script>

2, v-model and radio are used together

nbsp;html>


  
  Title


     

您选择的是:{{sex}}

  <script></script> <script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;你好&#39;, sex: &#39;女&#39; } }) </script>

3, v-model and CheckBox radio button are used together

nbsp;html>


  
  Title


        

您选择的是:{{isAgree}}

  
  <script></script> <script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;你好&#39;, isAgree: false } }) </script>

4. Use v-model in combination with CheckBox multi-select box

nbsp;html>


  
  Title


     <input>比比东   <input>千仞雪   <input>美杜莎   <input>云韵   <input>雅妃   

您选择的是:{{girls}}

  <script></script> <script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;你好&#39;, girls: [] } }) </script>

##5. Use v-model in combination with select
nbsp;html>


  <meta>
  <title>Title</title>


<div>
  <!-- 选择一个 -->
  <select>
    <option>云韵</option>
    <option>比比东</option>
    <option>雅妃</option>
    <option>千仞雪</option>
    <option>美杜莎</option>
  </select>
  <h3 id="您的选择是-girl">您的选择是:{{girl}}</h3>
</div>
 
<script></script>
<script>
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;,
      girl: &#39;云韵&#39;
    }
  })
</script>

6. v-for value binding

nbsp;html>


  
  Title


     

您的选择是:{{girls}}

  <script></script> <script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;你好&#39;, girls: [],//多选框 beautyGirls: ["云韵","比比东","雅妃","纳兰嫣然","美杜莎"] } }) </script>

##7. Use of v-model modifier

nbsp;html>


  
  Title


     <input>   

{{message}}

       <input>   

{{age}} --> {{typeof age}}

       <input>   

{{name}}

  <script></script> <script> const app = new Vue({ el: &#39;#app&#39;, data: { message: &#39;哪吒&#39;, age: 0, name: &#39;哪吒&#39; } }) </script>
3. Component development

Components are an important idea in Vue.js

It provides an abstraction, and we can develop an independent and repeatable Use small components to construct our application components
  • Can extend HTML elements and encapsulate reusable code
  • The component system allows us to use independent reusable small components to build large applications. The interface of almost any type of application can be abstracted into a component tree
  • Component-based thinking application

With the component-based thinking, we must fully implement it in our future development Use it
  • Split the page into small, reusable components as much as possible
  • This makes our code more convenient to organize and manage, and has strong scalability
1. Global component

nbsp;html>


  <meta>
  <title>Title</title>


<div>
  <my-cpn></my-cpn>
</div>
 
<script></script>
<script>
 
  //1.创建组件化构造器对象
  const cpnC = Vue.extend({
    template: `
      <div>
        <h2>我是标题
        <p>我是CSDN哪吒
      
      `
  })
 
  //2.注册组件
  Vue.component(&#39;my-cpn&#39;,cpnC)
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;
    }
  })
</script>

2. Local component

nbsp;html>


  <meta>
  <title>Title</title>


<div>
  <cpn></cpn>
</div>
 
<script></script>
<script>
 
  //1.创建组件化构造器对象
  const cpnC = Vue.extend({
    template: `
      <div>
        <h2>我是标题
        <p>我是CSDN哪吒
      
      `
  })
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;
    },
    components: {
      cpn: cpnC
    }
  })
</script>

3. Parent-child component

nbsp;html>


  <meta>
  <title>Title</title>


<div>
  <cpn2></cpn2>
</div>
 
<script></script>
<script>
 
  //1.创建组件化构造器对象
  const cpnC1 = Vue.extend({
    template: `
      <div>
        <h2>我是标题1
        <p>我是CSDN哪吒
      
      `
  })
 
  const cpnC2 = Vue.extend({
    template: `
      <div>
        <h2>我是标题2
        <p>我是博客专家
        <cpn1>
      
      `,
    components: {
      cpn1: cpnC1
    }
  })
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;
    },
    components: {
      cpn2: cpnC2
    }
  })
</script>

4、组件化语法糖写法

vue为了简化注册组件的过程,提供了语法糖的写法,主要是省去了调用Vue.extend()的步骤,直接使用一个对象替代。

nbsp;html>


  <meta>
  <title>Title</title>


<div>
  <my-cpn></my-cpn>
</div>
 
<script></script>
<script>
  //注册组件语法糖写法
  Vue.component(&#39;my-cpn&#39;,{
    template: `
      <div>
        <h2>我是标题
        <p>我是CSDN哪吒
      
      `
  })
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;
    }
  })
</script>

5、组件模板抽离写法

nbsp;html>


  <meta>
  <title>Title</title>


 
<div>
  <cpn></cpn>
  <cpn></cpn>
  <cpn></cpn>
</div>
 
<!--1.script标签, 注意:类型必须是text/x-template-->
<!--<script type="text/x-template" id="cpn">-->
<!--<div>-->
<!--<h2 id="我是标题">我是标题</h2>-->
<!--<p>我是CSDN哪吒</p>-->
<!--</div>-->
<!--</script>-->
 
<!--2.template标签-->
<template>
  <div>
    <h2 id="我是标题">我是标题</h2>
    <p>我是CSDN哪吒</p>
  </div>
</template>
 
<script></script>
<script>
 
  // 1.注册一个全局组件
  Vue.component(&#39;cpn&#39;, {
    template: &#39;#cpn&#39;
  })
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好啊&#39;
    }
  })
</script>
 

四、组件可以访问Vue实例数据吗?

1、简介

实验发现,组件不能访问Vue实例数据,而且即便可以访问,如果将所有的数据都放在Vue实例中,Vue实例就会变得非常臃肿。

Vue组件应该有自己保存数据的地方。

组件自己的数据存放在哪里?

  • 组件对象也有一个data属性(也有method等属性);
  • 只是这个data属性必须是一个函数;
  • 而且这个函数返回一个对象,对象内部保存着数据;

2、代码实例

nbsp;html>


  <meta>
  <title>Title</title>


 
<div>
  <cpn></cpn>
</div>
<template>
  <div>
    <h2 id="title">{{title}}</h2>
    <p>我是热门</p>
  </div>
</template>
 
<script></script>
<script>
 
  // 1.注册一个全局组件
  Vue.component(&#39;cpn&#39;, {
    template: &#39;#cpn&#39;,
    data() {
      return {
        title: &#39;哪吒必胜&#39;
      }
    }
  })
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;你好&#39;,
      // title: &#39;我是标题&#39;
    }
  })
</script>
 

3、效果展示 

五、父子组件通信

1、父子组件通信简介

在开发中,往往一些数据确实需要从上层传递到下层:

比如在一个页面中,我们从服务器请求到了很多的数据。

其中一部分数据,并非是我们整个页面的大组件来展示的,而是需要下面的子组件进行展示。

这个时候,并不会让子组件再次发送一个网络请求,而是直接让大组件(父组件)将数据传递给小组件(子组件)。

如何进行父子组件间的通信呢?Vue官方提到:

通过props向子组件传递数据

通过事件向父组件发送消息

在组件中,使用选项props来声明需要从父级接收到的数据。

props的值有两种方式:

方式一:字符串数组,数组中的字符串就是传递时的名称。

方式二:对象,对象可以设置传递时的类型,也可以设置默认值等。

2、父传子代码实例

nbsp;html>


  <meta>
  <title>Title</title>


 
<div>
  <!--<cpn v-bind:cgirls="girls"></cpn>-->
  <!--<cpn cgirls="girls" cmessage="message"></cpn>-->
 
  <cpn></cpn>
</div>
 
<template>
  <div>
    <ul>
      <li>{{item}}</li>
    </ul>
    <h2 id="cmessage">{{cmessage}}</h2>
  </div>
</template>
 
<script></script>
<script>
  // 父传子: props
  const cpn = {
    template: &#39;#cpn&#39;,
    // props: [&#39;cgirls&#39;, &#39;cmessage&#39;],
    props: {
      // 1.类型限制
      // cgirls: Array,
      // cmessage: String,
 
      // 2.提供一些默认值, 以及必传值
      cmessage: {
        type: String,
        default: &#39;aaaaaaaa&#39;,
        required: true
      },
      // 类型是对象或者数组时, 默认值必须是一个函数
      cgirls: {
        type: Array,
        default() {
          return []
        }
      }
    },
    data() {
      return {}
    },
    methods: {
 
    }
  }
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;CSDN哪吒&#39;,
      girls: [&#39;云韵&#39;, &#39;比比东&#39;, &#39;雅妃&#39;]
    },
    components: {
      cpn
    }
  })
</script>

3、父传子效果展示 

4、props中的驼峰标识

nbsp;html>


  <meta>
  <title>Title</title>


 
<div>
  <cpn></cpn>
</div>
 
<template>
  <div>
    <h2 id="cInfo">{{cInfo}}</h2>
    <h2 id="childMyMessage">{{childMyMessage}}</h2>
  </div>
</template>
 
<script></script>
<script>
  const cpn = {
    template: &#39;#cpn&#39;,
    props: {
      cInfo: {
        type: Object,
        default() {
          return {}
        }
      },
      childMyMessage: {
        type: String,
        default: &#39;&#39;
      }
    }
  }
 
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      info: {
        name: &#39;哪吒&#39;,
        age: 18,
        height: 1.88
      },
      message: &#39;csdn博客专家&#39;
    },
    components: {
      cpn
    }
  })
</script>
 

效果展示 

5、子传父(自定义事件方式)

自定义事件方式完成子传父。

什么时候需要自定义事件呢?

当子组件需要向父组件传递数据时,就要用到自定义事件了。

我们之前学习的v-on不仅仅可以用于监听DOM事件,也可以用于组件间的自定义事件。

自定义事件的流程:

  • 在子组件中,通过$emit()来触发事件。

  • 在父组件中,通过v-on来监听子组件事件。

6、子传父代码实例

nbsp;html>


  <meta>
  <title>Title</title>


 
<!--父组件模板-->
<div>
  <cpn></cpn>
</div>
 
<!--子组件模板-->
<template>
  <div>
    <button>
      {{item.name}}
    </button>
  </div>
</template>
 
<script></script>
<script>
 
  // 1.子组件
  const cpn = {
    template: &#39;#cpn&#39;,
    data() {
      return {
        categories: [
          {id: &#39;1&#39;, name: &#39;云韵&#39;},
          {id: &#39;2&#39;, name: &#39;比比东&#39;},
          {id: &#39;3&#39;, name: &#39;雅妃&#39;},
          {id: &#39;4&#39;, name: &#39;纳兰嫣然&#39;},
        ]
      }
    },
    methods: {
      btnClick(item) {
        // 发射事件: 自定义事件
        this.$emit(&#39;item-click&#39;, item)
      }
    }
  }
 
  // 2.父组件
  const app = new Vue({
    el: &#39;#app&#39;,
    data: {
      message: &#39;csdn哪吒&#39;
    },
    components: {
      cpn
    },
    methods: {
      cpnClick(item) {
        console.log(&#39;cpnClick&#39;, item);
      }
    }
  })
</script>
 

7、子传父效果展示

【相关推荐:javascript视频教程web前端

The above is the detailed content of Summary of Vue basic knowledge: Vue component development. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Netflix's Frontend: Examples and Applications of React (or Vue)Netflix's Frontend: Examples and Applications of React (or Vue)Apr 16, 2025 am 12:08 AM

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

The Frontend Landscape: How Netflix Approached its ChoicesThe Frontend Landscape: How Netflix Approached its ChoicesApr 15, 2025 am 12:13 AM

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

React vs. Vue: Which Framework Does Netflix Use?React vs. Vue: Which Framework Does Netflix Use?Apr 14, 2025 am 12:19 AM

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

The Choice of Frameworks: What Drives Netflix's Decisions?The Choice of Frameworks: What Drives Netflix's Decisions?Apr 13, 2025 am 12:05 AM

Netflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.

React, Vue, and the Future of Netflix's FrontendReact, Vue, and the Future of Netflix's FrontendApr 12, 2025 am 12:12 AM

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

Vue.js in the Frontend: Real-World Applications and ExamplesVue.js in the Frontend: Real-World Applications and ExamplesApr 11, 2025 am 12:12 AM

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

Vue.js and React: Understanding the Key DifferencesVue.js and React: Understanding the Key DifferencesApr 10, 2025 am 09:26 AM

Vue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.

Vue.js vs. React: Project-Specific ConsiderationsVue.js vs. React: Project-Specific ConsiderationsApr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor