Vue.js基础-09-组件(Component)

1. 注册组件

1.1 创建实例时注册

语法示例

  • 在实例中注册组件
      components: {
        tagName: {options,},
      }

tagName 为组件名,options 为配置选项。

  • 调用组件
<tagName></tagName>

示例

    <div id="app">
      <shu></shu>
    </div>

完整示例

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>CROW-宋</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <shu></shu>
  </div>

  <script>
    new Vue({
      el: "#app",
      components: {
        shu: {
          template: "<h1>自定义组件!</h1>",
        },
      },
    });
  </script>
</body>

</html>

1.2 在实例外注册组件

语法示例

  • 注册组件
Vue.component(tagName, options)

tagName 为组件名,options 为配置选项。

完整示例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>CROW-宋</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  </head>
  <body>
    <div id="app">
      <shu></shu>
    </div>

    <script>
      Vue.component("shu", {
        template: "<h1>自定义组件!</h1>",
      });
      new Vue({
        el: "#app",
      });
    </script>
  </body>
</html>

1.3 在变量中定义组件选项

完整示例

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>CROW-宋</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <shu></shu>
  </div>

  <script>
    var Child = {
      template: "<h1>自定义组件</h1>",
    };
    new Vue({
      el: "#app",
      components: {
        shu: Child
      },
    });
  </script>
</body>

</html>

2. 接收父组件传来属性(Prop)

子组件用它来接受父组件传递过来的数据的一个自定义属性。

父组件的数据需要通过 props 把数据传给子组件,子组件需要显式地用 props 选项声明 “prop”

2.1 基本使用

语法示例

        props: ["message"],

完整示例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>CROW-宋</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  </head>
  <body>
    <div id="app">
      <shu message="hello"></shu>
    </div>

    <script>
      Vue.component("shu", {
        propes: ["message"],
        template: "<h1>{{message}}</h1>",
      });
      new Vue({
        el: "#app",
      });
    </script>
  </body>
</html>

2.2 动态绑定

用 v-bind 动态绑定 props 的值到父组件的数据中。每当父组件的数据变化时,该变化也会传导给子组件:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>CROW-宋</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <div>
      <input v-model="name">
      <br>
      <shu v-bind:message="name"></shu>
    </div>
  </div>

  <script>
    Vue.component('shu', {
      props: ['message'],
      template: '<span>{{ message }}</span>'
    })
    new Vue({
      el: '#app',
      data: {
        name: '关羽'
      }
    })
  </script>
</body>

</html>
  • 结果显示

2.3 绑定列表

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>CROW-宋</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <ol>
      <todo-item v-for="item in sites" v-bind:todo="item"></todo-item>
    </ol>
  </div>

  <script>
    Vue.component('todo-item', {
      props: ['todo'],
      template: '<li>{{ todo.name }}</li>'
    })
    new Vue({
      el: '#app',
      data: {
        sites: [
          { name: '刘备' },
          { name: '关羽' },
          { name: '张飞' }
        ]
      }
    })
  </script>
</body>

</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值