Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue $props 对象


示例

使用 $props 对象来显示接收到的属性。

<template>
  <div>
    <h3>Received Props</h3>
    <p>This is the $props object:</p>
    <pre>{{ this.$props }}</pre>
  </div>
</template>
运行示例 »

查看下面更多示例。


定义和用法

The $props 对象代表组件中声明的属性,以及当前的值。

Vue 中的属性是一种将值作为属性传递给子组件的方式。 查看 Vue 属性教程页面.

The $props 对象可用于例如将属性传递给下一个子组件(参见下面示例 1),或例如根据属性设置计算属性(参见下面示例 2)。

The $props 对象是只读的。


更多示例

示例 1

使用 $props 对象将属性传递给下一个子组件。

<template>
  <div>
    <h3>InfoBox.vue</h3>
    <p>This is the $props object that is received from App.vue and passed down to the next child component:</p>
    <pre>{{ this.$props }}</pre>
    <grand-child v-bind="$props" />
  </div>
</template>

<script>
export default {
  props: [
    'bagOwner',
    'bagWeight'
  ]
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  margin-top: 20px;
  max-width: 370px;
}
</style>
运行示例 »

示例 2

在计算属性中使用 $props 对象,根据袋子的重量创建反馈消息。

<template>
  <div>
    <h3>InfoBox.vue</h3>
    <p>The $props object is used in a computed value to create a message based on the weight of the bag:</p>
    <span>{{ this.bagWeightStatus }}</span>
  </div>
</template>

<script>
export default {
  props: [
    'bagWeight'
  ],
  computed: {
    bagWeightStatus() {
      if(this.$props.bagWeight>10) {
        return 'Puh, this bag is heavy!'
      }
      else {
        return 'This bag is not so heavy.'
      }
    }
  }
}
</script>

<style scoped>
div {
  border: solid black 1px;
  padding: 10px;
  max-width: 350px;
  margin-top: 20px;
}
span {
  background-color: lightgreen;
  padding: 5px 10px;
  font-weight: bold;
}
</style>
运行示例 »

相关页面

Vue 教程: Vue 组件

Vue 教程: Vue 计算属性

Vue 教程: Vue 属性

Vue 教程: Vue v-bind 指令


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.