今天來給你們說說父子組件傳值,認真了認真了,拿好小本本。javascript
vue init webpack-simple template
cd template
npm i
npm run devhtml
方式一 :子組件直接訪問父組件的數據vue
- 父組件在調用子組件時,綁定想要獲取的父組件中的數據
- 在子組件內部,使用 props 選項聲明獲取的數據,即接收來自父組件中的數據
建立以下目錄java
App.vue 中寫入webpack
<template> <div class="hello"> <h3>我是 App 父組件</h3> <h4>訪問本身的數據:{{msg}},{{name}},{{user.id}}</h4> <hr> <!-- 1. 在調用子組件時,綁定想要獲取的父組件中的數據 --> <Hello :message="msg" :name="name" :user="user"></Hello> </div> </template> <script> // 引入 Hello 組件 import Hello from './assets/components/Hello.vue' export default { data(){ return { msg:'父組件', name:'tom', age:'22', user:{ id:1234, userName:'Jack' } } }, // 註冊 Hello 組件 components:{ Hello } } </script>
Hello.vue 文件中寫入web
<template> <div class="hello"> <h3>我是 hello 子組件</h3> <!-- 在頁面中直接渲染便可 --> <h4>訪問父組件中的數據: {{message}},{{name}},{{user.id}}</h4> </div> </template> <script> export default { // 2. 在子組件內部,使用 props 選項聲明獲取的數據,即接收來自父組件中的數據 props:['message','name','user'] } </script>
最後效果:npm
方式二 :爲組件的 prop 指定 驗證 規則,若是傳入的數據不符合要求,Vue 會發出警告數組
- 父組件在調用子組件時,綁定想要獲取的父組件中的數據
- 在 props 內以對象的形式寫入校驗規則
App.vue 中寫入ide
<template> <div class="hello"> <h3>我是 App 父組件</h3> <h4>訪問本身的數據:{{msg}},{{name}},{{user.id}}</h4> <hr> <!-- 1. 在調用子組件時,綁定想要獲取的父組件中的數據 --> <Hello :message="msg" :name="name" :age="age" :user="user" :money="money"></Hello> </div> </template> <script> // 引入 Hello 組件 import Hello from './assets/components/Hello.vue' export default { data(){ return { msg:'父組件', name:'tom', age:'22', user:{ id:9876, userName:'Jack' }, money:'123' } }, // 註冊 Hello 組件 components:{ Hello } } </script>
Hello.vue 中寫入函數
<template> <div class="hello"> <h3>我是 hello 子組件</h3> <!-- 在頁面中直接渲染便可 --> <h4>訪問父組件中的數據: {{message}} <br> {{name}}<br> {{user.id}}<br> {{user.userName}}<br> {{age}}<br> {{ageOne}}<br> {{money}}<br> </h4> </div> </template> <script> export default { props:{ // 基礎類型檢測 (`null` 指容許任何類型) message:String, // 多是多種類型 name:[String,Number], // 必傳且是字符串 age:{ type:String, required:true }, // 數值且有默認值 若是父組件中沒有該數據綁定,顯示如下的默認值 ageOne:{ type: Number, default: 10 }, // 數組/對象的默認值應當由一個工廠函數返回 user:{ type:Object, default:function(){ return { userName: 'Doctor' } } }, // 自定義驗證函數 money:{ validator:function(value){ return value > 100 } } } } </script>
效果以下
注意:Prop 是單向綁定的:當父組件的屬性變化時,將傳導給子組件,可是反過來不會。這是爲了防止子組件無心間修改了父組件的狀態,來避免應用的數據流變得難以理解。
另外,每次父組件更新時,子組件的全部 prop 都會更新爲最新值。這意味着你不該該在子組件內部改變 prop。
- 在子組件中使用 $emit(事件名,數據) 觸發一個自定義事件發送數據
- 在父組件在使用子組件的標籤內監聽子組件的觸發事件,並在父組件中定義方法用來獲取數據
在 Hello.vue 中寫入
<template> <div class="hello"> <h3>我是 hello 子組件</h3> <h4>訪問本身的數據: {{msg}} <br> {{name}} </h4> <!-- 觸發 send 事件 ,發送數據 --> <button @click="send">將子組件中的數據發送給父組件</button> </div> </template> <script> export default { data(){ return { msg:'子組件', name:'tom' } }, methods:{ // 在此處定義事件,用來發送數據,也可直接寫到 mounted 內自動發送 send(){ // 此處的 this 表示當前子組件的實例 this.$emit('hello',this.msg,this.name) } } } </script>
在 App.vue 中寫入
<template> <div class="hello"> <h3>我是 App 父組件</h3> <!-- 6. 在頁面中渲染 --> <h4>訪問子組件的數據:{{msg}},{{name}}</h4> <hr> <!-- 子組件 --> <!-- 3. 在子組件標籤內監聽子組件事件的觸發 --> <Hello @hello="getData"></Hello> </div> </template> <script> // 引入 Hello 組件 import Hello from './assets/components/Hello.vue' export default { data(){ return { // 4. 初始化數據對象 msg:'', name:'', } }, methods:{ // 5. 接收子組件傳過來的數據 getData(msg,name){ this.msg = msg, this.name = name } }, // 註冊 Hello 組件 components:{ Hello } } </script>
效果圖: