1. 程式人生 > >vue父傳子

vue父傳子

span import console created urn nbsp ted col child

父組件傳遞數據給子組件用props,父組件中使用子組件,子組件用props接收父組件數據。

Home父組件代碼:

<template>
<div>
     {{test}}
     <!-- 使用子組件,綁定父組件數據數據 -->
    <Child :test="test"></Child>
</div>
</template>
<script>
// import子組件
import Child from ‘./Child.vue‘
export default {
  name: "Home",
  
//components引入子組件 components:{ Child }, data () { return { test:123 }; } } </script> <style lang="css" scoped> </style>

Child子組件代碼:

<template>
<div>
    <!-- 使用子組件數據 -->
     {{test}}
</div>
</template>
<script>
export default
{ name: "Child", // props使用獲取父組件數據 props:["test"], data () { return { }; }, created(){ // 使用子組件數據 console.log(this.test); } } </script> <style lang="css" scoped> </style>

vue父傳子