Vue全局組件注冊


通過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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM