利用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