黄子涵

传入一个数组

<!-- 即便数组是静态的,我们仍然需要 `v-bind` 来告诉 Vue -->
<!-- 这是一个 JavaScript 表达式而不是一个字符串。-->
<blog-post v-bind:comment-ids="[234, 266, 273]"></blog-post>

<!-- 用一个变量进行动态赋值。-->
<blog-post v-bind:comment-ids="post.commentIds"></blog-post>
<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>传入一个数组</title>
  <script src="./vue.js"></script>
</head>

<body>
  <div id="hzh">
    <hzh-component v-bind:comment-ids="[234, 266, 273]"></hzh-component>
  </div>
  <script>
    Vue.component('hzh-component', {
      props: ['comment-ids'],
      template: '<h3>评论的ID号:{{commentIds}}</h3>'
    })

    new Vue({
      el: '#hzh',
    })
  </script>
</body>

image

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>传入一个数组</title>
  <script src="./vue.js"></script>
</head>

<body>
  <div id="hzh">
    <hzh-component v-for="post in posts" v-bind:key="post.id" v-bind:comment-ids="post.commentIds"></hzh-component>
  </div>
  <script>
    Vue.component('hzh-component', {
      props: ['commentIds'],
      template: '<h3>评论的ID号:{{commentIds}}</h3>'
    })

    new Vue({
      el: '#hzh',
      data: {
        posts: [
          { id: 1, commentIds: [234, 266, 273]}
        ]
      }
    })
  </script>
</body>

image

posted @ 2022-06-05 10:43  黄子涵  阅读(27)  评论(0编辑  收藏  举报