Vue全局组件与局部组件

       全局注册组件,组件在注册之后可以用在任何新创建的 Vue 根实例的模板中使用,局部注册的组件只能在指定父组件下使用,作用域小。

<template>
  <div id="app">
    <div>
      <div>{{msg}}</div>
      <child-one></child-one>
      <child-two> </child-two>
      <global-component></global-component>
    </div>
    <global-component></global-component>
  </div>
</template>

<script>
  import Vue from 'vue'

  // 定义全局组件global-component,可以用于根节点下的任何地方
  Vue.component('global-component', {
    template: "<div>{{globalTitle}}</div>",
    data: function() {
      return {
        globalTitle: '我是全局组件'
      }
    }
  })

  export default {
    name: 'App',
    data() {
      return {
        msg: '研究全局组件与局部组件'
      }
    },
    //定义一个局部子组件
    components: {
      'child-one': {
        template: '<div>{{oneTitle}}</div>',
        //子组件的数据必须以函数返回值的形式定义,不可以使用对象定义
        data: function() {
          return {
            oneTitle: '我是局部子组件child-one'
          }
        }
      },
      'child-two': {
        template: '<div>{{towTitle}}</div>',
        data: function() {
          return {
            towTitle: '我是局部子组件child-two'
          }
        }
      }
    }
  }
</script>
<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值