search
HomeWeb Front-endVue.jsWhat does prop mean in vuejs
What does prop mean in vuejsSep 18, 2021 pm 06:27 PM
propvuejs

In vuejs, prop is a custom property used by the parent component to pass data. Subcomponents need to explicitly declare "prop" using the props option; when using non-string templates, the prop name form will be converted from camelCase to kebab-case (dash separated).

What does prop mean in vuejs

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

1. Use Prop to pass data

The scope of component instances is isolated. This means that the parent component's data cannot and should not be referenced directly within the child component's template. You can use props to pass data to child components.
prop is a custom property used by the parent component to pass data. The child component needs to explicitly declare "prop" with the props option

Vue.component('child',{
    props:['message'],
    template:&#39;<span>{{ message }}</span>&#39;
})

and then pass it a normal string:

<child message="hello!"></child>

Result:

hello!

2. camelCase vs.kebab-case

html is not case sensitive. When using non-string templates, the prop name format will be converted from camelCase to kebab-case (separated by dashes):

//camelCase
Vue.component(&#39;child&#39;,{
    props:[&#39;myMessage&#39;],
    template:&#39;<span>{{ message }}</span>&#39;
})
//kebab-case
<child my-message="hello!"></child>

Again, if you are using string templates, don't worry about these restrictions. .

3. Dynamic Prop

is similar to using v-bind to bind HTML features to an expression. You can also use v -bindDynamicly bind the value of props to the data of the parent component. Whenever the data of the parent component changes, the change is also propagated to the child component.

<div>
    <input v-model="parrentMsg">
    <br>
    <child v-bind:my-message="parrentMsg"></child>
</div>

Using the abbreviated syntax of v-bind is usually simpler:

<child :my-message="parrentMsg"></child>

4. Literal syntax vs dynamic syntax

Because it is a literal prop, its value is passed as the string "1" rather than as an actual number. If you want to pass an actual JavaScript number, you need to use v-bind so that its value is evaluated as a JavaScript expression:

5. Single data Flow

prop is single-bound: when the properties of the parent component change, it will be transmitted to the child component, but not the other way around. This is to prevent child components from inadvertently modifying the parent component's state - which would make the application's data flow difficult to understand. At the same time, this is also easy to understand. The parent component is a high-level abstraction of the sub-component, representing the common parts of the sub-component. Changes in the data of one component will not change its abstraction, but changes in its abstraction represent changes in all sub-components.
In addition, every time the group is gradually updated, all props of the subcomponent will be updated to the latest values. This means you should not change props inside child components. If you do this, Vue will give a warning in the console.
There are usually two cases of changing prop:

1.prop is passed in as the initial value, and the subcomponent will only use its initial value as the initial value of local data;

2.prop is passed in as the original value that needs to be transformed.

More precisely, these two situations are:
a. Define a local data attribute and use the initial value of prop as the initial value of local data.

props: [‘initialCounter’], 
 data: function () { 
 return { counter: this.initialCounter} 
 }
    b.定义一个 computed 属性,此属性从 prop 的值计算得出。
   ```
    props: [&#39;size&#39;],
    computed: {
        normalizedSize: function () {
         return this.size.trim().toLowerCase()
     }
    }

6. Prop verification

The component can specify verification requirements for props. Vue will issue a warning if validation requirements are not specified. This is useful when the component is made available to others.
When prop is an object rather than a string array, it contains validation requirements:

Vue.component(&#39;example&#39;, {
  props: {
    // 基础类型检测 (`null` 意思是任何类型都可以)
    propA: Number,
    // 多种类型
    propB: [String, Number],
    // 必传且是字符串
    propC: {
      type: String,
      required: true
    },
    // 数字,有默认值
    propD: {
      type: Number,
      default: 100
    },
    // 数组/对象的默认值应当由一个工厂函数返回
    propE: {
      type: Object,
      default: function () {
        return { message: &#39;hello&#39; }
      }
    },
    // 自定义验证函数
    propF: {
      validator: function (value) {
        return value > 10
      }
    }
  }
})

type can be the following native constructor:

* String

* Number

* Boolean

* Function

* Object

* Array

type can also be a custom constructor, detected using instanceof. When prop validation fails, a warning will be thrown if you are using the development version.

Related recommendations: "vue.js Tutorial"

The above is the detailed content of What does prop mean in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Flask + Vue.js:快速实现单页面应用Flask + Vue.js:快速实现单页面应用Jun 17, 2023 am 09:06 AM

随着移动互联网和Web技术的迅速发展,越来越多的应用需要提供流畅、快速的用户体验。传统的多页面应用已经无法满足这些需求,而单页面应用(SPA)则成为了解决方案之一。那么,如何快速实现单页面应用呢?本文将介绍如何利用Flask和Vue.js来构建SPA。Flask是一个使用Python语言编写的轻量级Web应用框架,它的优点是灵活、易扩

VUE3快速入门:使用Vue.js指令实现选项卡切换VUE3快速入门:使用Vue.js指令实现选项卡切换Jun 15, 2023 pm 11:45 PM

本文旨在帮助初学者快速入手Vue.js3,实现简单的选项卡切换效果。Vue.js是一个流行的JavaScript框架,可用于构建可重用的组件、轻松管理应用程序的状态和处理用户界面的交互操作。Vue.js3是该框架的最新版本,相较于之前的版本变动较大,但基本原理并未改变。在本文中,我们将使用Vue.js指令实现选项卡切换效果,目的是让读者熟悉Vue.js的

VUE3基础教程:使用Vue.js插件封装图片上传组件VUE3基础教程:使用Vue.js插件封装图片上传组件Jun 15, 2023 pm 11:07 PM

VUE3基础教程:使用Vue.js插件封装图片上传组件Vue.js是一款流行的前端框架,它使开发者可以用更少的代码创建更高效、灵活的应用程序。尤其是在Vue.js3发布之后,它的优化和改进使得更多的开发者倾向于使用它。这篇文章将介绍如何使用Vue.js3来封装一个图片上传组件插件。在开始之前,需要先确保已经安装了Vue.js和VueCLI。如果尚未安装

VUE3基础教程:使用Vue.js插件封装日历组件VUE3基础教程:使用Vue.js插件封装日历组件Jun 15, 2023 pm 09:09 PM

Vue.js是现代化的前端JavaScript框架之一,它提供了一套完整的工具来构建交互式用户界面。在Vue.js的生态系统中,有各种各样的插件和组件,可以大大简化我们的开发流程。在本篇文章中,我们将介绍如何使用Vue.js插件封装一个日历组件,以方便我们在Vue.js项目中快速使用。Vue.js插件Vue.js插件可以扩展Vue.js的功能。它们可以添加全

Vue.js实现登录验证的完整指南(API、JWT、axios)Vue.js实现登录验证的完整指南(API、JWT、axios)Jun 09, 2023 pm 04:04 PM

Vue.js是一种流行的JavaScript框架,用于构建动态Web应用程序。实现用户登录验证是开发Web应用程序的必要部分之一。本文将介绍使用Vue.js、API、JWT和axios实现登录验证的完整指南。创建Vue.js应用程序首先,我们需要创建一个新的Vue.js应用程序。我们可以使用VueCLI或手动创建一个Vue.js应用程序。安装axiosax

VUE3开发入门教程:使用Vue.js组件封装chart图表VUE3开发入门教程:使用Vue.js组件封装chart图表Jun 15, 2023 pm 10:29 PM

随着大数据时代的到来,数据可视化已经成为了现如今的趋势之一。在Web前端开发的过程中,如何使用Vue.js进行数据可视化处理,成为了许多前端开发者所关注的问题。本文将会介绍如何使用Vue.js组件,封装基于chart.js库的图表。1.了解chart.jsChart.js是一款基于HTML5CanvasElement的简单易用、跨平台的开源图表库,我们可

VUE3基础教程:使用Vue.js自定义事件VUE3基础教程:使用Vue.js自定义事件Jun 15, 2023 pm 09:43 PM

Vue.js是一款流行的JavaScript框架,它提供了很多方便的特性,所以它在开发Web应用程序时非常有用。Vue.js中的自定义事件系统使其更加灵活,并且可以通过组件事件触发和处理来实现更好的代码重用性。在本文中,我们将讨论如何使用Vue.js的自定义事件。Vue.js中自定义事件的基础在Vue.js中,我们可以通过v-on指令来监听DOM事件。例如,

使用Python与Vue.js开发实时同步的Web应用程序使用Python与Vue.js开发实时同步的Web应用程序Jun 17, 2023 am 08:28 AM

随着Web应用程序的普及和用户体验的要求不断提高,实时同步已经成为了现代Web应用程序不可或缺的功能。在本文中,我们将介绍如何使用Python和Vue.js开发实时同步的Web应用程序。为了实现实时同步的功能,我们需要使用一些现代化的Web技术,其中包括WebSocket、异步编程和前端框架。以下是本文中将用到的技术栈:Python3.6+FlaskFla

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!