Vue parent component to pass value study notes to child components

     The function is to pass the name in the Vue global instance (parent component) to the child component 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>

<!--通过id=app将父组件的name通过动态:Name传值到子组件,因此子组件用的是Name而不是name-->
<div id="app" >
  <zi :Name="name"></zi>
</div>

<!--值在子组件的template中使用,见下的{
   
   {Name}}-->
<template id="name">
  <div>
    <h1>我是{
   
   {Name}}</h1>
  </div>
</template>
</body>

<script src="js/vue.js"></script>

<script>
  const zi={
    template:`#name`,
    props: ['Name']
    //props在子组件中声明(注意此时的名Name不是父组件中的name)、子组件用的时候使用props中的声明Name
  }
</script>

<script>
  const vue= new Vue({
    el: '#app',
    data:{
      name: "ZTY"
    },
    components:{
      zi
    }
  })
</script>
</html>

 

Guess you like

Origin blog.csdn.net/Zhongtongyi/article/details/109306310