vue全域性元件和區域性元件的寫法

2021-09-10 19:47:44 字數 1166 閱讀 3247

** 感謝博主分享

vue元件有兩種,一種是全域性元件,一種是區域性元件。整個專案經常用到的用全域性寫法,用到比較少的專供特定頁面用的使用區域性元件。

全域性元件引入寫法:在專案的main.js中:

import vue from

'vue'

;import mycomponent from

'@/components/mycomponent.vue'

;// 匯入自己寫的元件檔案

vue.

use(mycomponent)

;// 自定義全域性元件的時候需要vue.use();

vue.

component

('my-component'

, mycomponent)

;//初始化元件

newvue(,

template:,}

);

區域性元件引入寫法:在需要使用元件的a.vue檔案中:

<

/template>

import mycomponent from

'@/components/mycomponent.vue'

;export

default

,// 直接初始化就可以啦,不需要vue.use();

data()

, methods:};

<

/script>

"scss" scoped>

<

/style>

下面附上自定義元件的mycomponent.vue檔案**:

"methods1()"

>

<

/a>

<

/div>

<

/template>

import

from

'mint-ui'

;export

default};

},props:

, methods:}}

;<

/script>

"scss" scoped>

<

/style>

vue全域性元件和區域性元件

vue元件包括兩種 1 全域性元件,2 區域性元件 區域性元件var 變數名字一定要放在new vue例項外面 1 全域性元件 text align center color red font size 20px margin top 20px 全域性元件直接像上面這樣 註冊 就可以 如果還想註冊繼...

vue 全域性元件和區域性元件

vue.component mycpn cpnc components html lang en head meta charset utf 8 title document title script src js vue.js script head div id mycpn div div id...

Vue 元件(一)全域性元件和區域性元件

根據vue官方文件 元件 component 是vue.js最強大的功能之一。元件可以擴充套件html元素,封裝可重用的 在較高層面上,元件是自定義元素。按我當前學習程度的理解,元件就是定義乙個自己的新標籤,新標籤可以在html 中使用。全域性宣告元件 vue.component my compon...