1. 程式人生 > >Vue全局組件註冊

Vue全局組件註冊

對象 span cli 註冊 使用 component -c nbsp urn

通過Vue.component(‘組件名’, {配置對象})註冊全局組件

在main.js中註冊全局組件 test

import Vue from ‘vue‘
import App from ‘./App.vue‘

//全局組件,定義後可直接使用,無需引入
Vue.component(‘test‘, {
  data () {
    return {
      count: 0
    }
  },
  template: ‘<button @click="count++">點擊次數{{count}}</button>‘
})

new Vue({ el:‘#app‘, template: ‘<App />‘, components: {App} })

在App組件中使用 test 組件

  不需要另外引入,直接使用即可

<template>
    <div>
      <test />
  </div>
</template>

<script>
  export default {
    data () {
      return { }
    }
  }
</
script> <style> </style>

Vue全局組件註冊