鍍金池/ 問答/C++  HTML/ vue中的組件注冊和文檔中的區(qū)別?

vue中的組件注冊和文檔中的區(qū)別?

vue官方文檔提供的組件方式是:

Vue.component('my-component', {
  code here
})

一些項目中的組件形式

<template lang="html">

</template>

<script>
    export default{
      
    }
</script>

<style lang="css">

</style>

這兩種組件創(chuàng)建的方式有什么不一樣嗎?

回答
編輯回答
夢若殤

你可以查一下vue單文件組件,這也是項目中最常使用的組件組織形式。

2018年3月17日 00:40
編輯回答
殘淚

第一種方式是全局注冊組件,第二種方式不在main里注冊的話一般是局部注冊組件。

2018年2月27日 12:00
編輯回答
局外人
  • 這是全局注冊組件。

    Vue.component('my-component', {
      code here
    })
  • 這是單文件組件,聲明了一個組件的表現(xiàn)形式(實例),還沒有完成注冊,你可以把單文件組件里的代碼當(dāng)作Vue.component()的第二個參數(shù)里的東西。

    // demo
    <template lang="html">
    </template>
    <script>
        export default{
        }
    </script>
    <style lang="css">
    </style>

    使用的時候進(jìn)行注冊,可以使用Vue.component('el-demo', Demo)或者components: {Demo}的形式進(jìn)行注冊。

  • 你所看到的大部分的組件庫,使用說明里都有Vue.use(組件庫)的方式,就是在進(jìn)行Vue.component()全局注冊,而按需加載,用哪個引哪個import Button from 'xxx', components: { Button }就是另一種了。
2018年3月13日 10:55