SoFunction
Updated on 2025-03-10

Detailed explanation of Vue's knowledge points about component development

Global component registration

('first-component', {
 data: function () {
  return {
   count: 0
  }
 },
 template: '<button @click="count++">{{ count }}</button>'
})

data must be a function

The component template content must be a single root element

The component template content can be a template string

Global components can be nested

Component naming method

('first-component', {/* .... */})
// Camel cannot be used in ordinary tag templates, only camel method can be used in template('firstComponent', {/* .... */})

Local component registration

Partially registered components can only be used in parent components;

var vm = new Vue({
 components: {
  'hello-world': {
   data: function () {
    return {
     msg: 'hello world'
    }
   },
   template: '<div>{{ msg }}</div>'
  }
 }
})

props data delivery principle: One-way data flow

The value passed through props is received within the component

('son-com', {
	props: ['msg', 'parentMsg']
  template: '<div>{{msg + "---" + parentMsg}}</div>'
})

Parent component passes value to child component through attributes

&lt;son-com msg="The value of the parent component" :parent-msg="The value of the parent component bound to bind"&gt;&lt;/son-com&gt;

props attribute name rules

  • Use camel form in props, and short horizontal lines are needed in templates; html is insensitive to case
  • There is no limit in strings

props delivery type

&lt;div &gt;
 &lt;son-com
  :str="pstr"
  :num="pnum" &lt;!-- Note if not v-bind The exact attribute value cannot be obtained --&gt;
  :boolean="pboolean"
  :arr="parr"
  :obj="pobj"
 &gt;
 &lt;/son-com&gt;
&lt;/div&gt;
('son-com', {
 props: ['str', 'num', 'boolean', 'arr', 'obj'],
 template: `
  <div>
   <div>{{ str }}</div>
   <div>{{ num }}</div>
   <div>{{ boolean }}</div>
   <ul>
    <li :key="index" v-for="(item, index) in arr">{{ item }}</li>
   </ul>
   <div>
    <span>{{  }}</span>
    <span>{{  }}</span>
   </div>
  </div>
 `
})
var vm = new Vue({
 el: '#app',
 data: {
  pstr: 'hello Vue',
  pnum: 12,
  pboolean: true,
  parr: ['apple', 'banner', 'orange'],
  pobj: {name: 'zs', age: 22}
 }
})

Subcomponent pass values ​​to parent component

The child component passes the value $emit() to the parent component through a custom event

('son-com', {
 template: `
  &lt;div&gt;
   &lt;button @click="$emit('parent')"&gt;Click to enlarge the parent component font&lt;/button&gt;
   The value is transferred from the second parameter
	 &lt;button @click="$emit('parent', 10)"&gt;Click to enlarge the parent component font&lt;/button&gt;
  &lt;/div&gt;
 `
})

Parent component listens for child component events

&lt;div &gt;
 &lt;div&gt;Parent component&lt;/div&gt;
 &lt;son-com @parent="handle"&gt;&lt;/son-com&gt;
 &lt;!-- The received value is fixed $event--&gt;
 &lt;son-com @parent="handle($event)"&gt;&lt;/son-com&gt;
&lt;/div&gt;
var vm = new Vue({
 el: '#app',
 data: {
  font: 10
 },
 methods: {
  handle: function (val) {
    += 5
    += val // At this time, val is the value passed by the child component  }
 },
})

Non-parent-child component passes value

Communication between separate event center management components

// Create Event Centervar hub = new Vue()

// Listen to events in mountedhub.$on('eventName', fn)
hub.$off('eventName') // Destruction incident
// Handle events in methodshub.$emit('eventName', param)

Component slots

&lt;tmp-com&gt;
 &lt;!-- Only one tag can be matched --&gt;
 &lt;p slot="header"&gt;Program error&lt;/p&gt;
 &lt;div&gt;I don't have a match&lt;/div&gt;
 &lt;!-- Can match multiple tags --&gt;
 &lt;template slot="footer"&gt;
  &lt;p&gt;Match the footer once&lt;/p&gt;
  &lt;p&gt;Match the footer twice&lt;/p&gt;
 &lt;/template&gt;
&lt;/tmp-com&gt;
('tmp-com', {
 template: `
  &lt;div&gt;
   &lt;header&gt;
    &lt;slot name="header"&gt;&lt;/slot&gt;
   &lt;/header&gt;
   &lt;div&gt;
    If the corresponding tag is not matched above, the default content will be displayed
    &lt;slot&gt;&lt;/slot&gt;
   &lt;/div&gt;
   &lt;footer&gt;
    &lt;slot name="footer"&gt;&lt;/slot&gt;
   &lt;/footer&gt;
  &lt;/div&gt;
 `
})

This is the end of this article about Vue's detailed explanation of knowledge points on component development. For more related Vue component development content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!