vue父子組件之間的通信


利用props在子組件接受父組件傳過來的值
1.父組件parentComp.vue

 1 <template>
 2   <childComp :fromParentToChild="fromParentToChild"></childComp>
 3 </template>
 4 <script>
 5 import childComp from './childComp.vue'
 6 export default{
 7   data(){
 8   return{
 9   fromParentToChild:"I am from Parent"
10   }
11   },
12   components:{childComp}
13   }
14 </script>

 


2.子組件childComp.vue

 1 <template>
 2 <div>{{fromParentToChild}}</div>
 3 </template>
 4 <script>
 5 export default{
 6 props:['fromParentToChild'],
 7 data(){
 8 console.log(this.fromParentToChild)
 9 return{
10   }
11   }
12   }
13 </script>

 


3.路由文件index.js

export default new Router({
routes: [
{
path:'/parentToChild',
name:'parentToChild',
component:require('../components/demo/parentComp.vue')
}})

 


在瀏覽器地址欄輸入:http://localhost:[port]/#/parentToChild
可以在頁面窗口看到顯示:I am from Parent


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM