vue 父组件向子组件传值

  父组件向子组件传值

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 400px;
        height: 600px;
        margin: 0 auto;
        overflow-x: hidden;
        overflow-y: auto;
        border: 1px solid black;
      }
      textarea {
        width: 300px;
        height: 50px;
        margin-top: 10px;
        margin-left: 10px;
      }
      .q1 {
        width: 50px;
        height: 50px;
        position: relative;
        top: -20px;
        left: 10px;
      }
      .box1 {
        width: 400px;
        height: 90px;
        margin: 0;
        border: 1px solid black;
      }

      .box2 {
        width: 100%;
        height: 50px;
        border: 1px solid black;
        background-color: antiquewhite;
        overflow: hidden;
        margin: 10px 0;
      }

      textarea {
        resize: none;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="box">
        <div class="box1">
          <textarea name="" id="" v-model="msg"></textarea
          ><button class="q1" @click="public1">发表</button>
        </div>
        <div class="aa">
          <top v-bind:msg="lists"></top>
        </div>
        <p style="text-align: center" v-else>暂无内容</p>
      </div>
    </div>
  </body>
  <script src="./vue.min.js"></script>
  <script>
    Vue.component("top", {
      props: ["msg"],
      data() {
        return {
          lists: [],
        };
      },
      template: `
     <div >
      <div class="box2" v-for='(a,index) in msg' :key='index'>{{a}}</div>
       </div>
      `,
    });
    var app = new Vue({
      el: "#app",

      data: {
        msg: "",
        lists: [],
      },
      methods: {
        public1() {
          if (this.msg == "") {
          } else {
            this.lists.push(this.msg);
            console.log(this.lists);
            this.msg = "";
          }
        },
      },
    });
  </script>
</html>

                1 在子组件中写上props 在props中自定义名字

                props: ['自定义名字']

                2 在父组件中找到子组件标签 然后在子组件标签上 使用v-bind:自定义名字="父组件要发送的数据"

                <子组件标签 :自定义名字="要发送的数据"></子组件标签>

                3 在子组件中将自定义名字当做data使用

                <元素标签>{{自定义名字}}</元素标签>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值