黄子涵

传入一个数字

<!-- 即便 `42` 是静态的,我们仍然需要 `v-bind` 来告诉 Vue -->
<!-- 这是一个 JavaScript 表达式而不是一个字符串。-->
<blog-post v-bind:likes="42"></blog-post>

```html
<!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:likes="42"></hzh-component>
  </div>
  <script>
    Vue.component('hzh-component', {
      props: ['likes'],
      template: '<h3>点赞数:{{likes}}</h3>'
    })

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

image

<!-- 用一个变量进行动态赋值。-->
<blog-post v-bind:likes="post.likes"></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-for="post in posts" v-bind:key="post.id" v-bind:likes="post.likes"></hzh-component>
  </div>
  <script>
    Vue.component('hzh-component', {
      props: ['likes'],
      template: '<div>点赞数:{{likes}}</div>'
    })

    new Vue({
      el: '#hzh',
      data: {
        posts: [
          { id: 1, likes: 42}
        ]
      }
    })
  </script>
</body>

image

posted @ 2022-06-04 18:31  黄子涵  阅读(22)  评论(0编辑  收藏  举报