1. 程式人生 > >Vue元件通訊(props) 父傳子

Vue元件通訊(props) 父傳子

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue.js"></script>
  </head>
  <body>
    <div id
="app">
<!-- 元件 --> <father></father> </div> <script> // 子元件 let son = { template: '<div>哇老爹給我傳了{{sonmoney}}個面板,開森^_^</div>', // 使用props定義獲取 props: ['sonmoney'] } // 建立全劇組建 // <son :sonmoney="money"></son> 和上面props: ['sonmoney']對應
Vue.component('father', { template: ` <div> <h1>瓜娃子我是你爹,我有{{money}}面板傳給你啊</h1> <son :sonmoney="money"></son> </div> `, components: { son }, // data必須是一個函式,要返回一個新的物件 data () {
return { money: 365 } } }) var vm = new Vue({ el: '#app', data: { } })
</script> </body> </html>