Vue組件如何自動按需引入詳析
在Vue中我們可以通過全局組件、局部注冊的方式來使用組件
全局注冊
通過app.component來創(chuàng)建全局組件
import { createApp } from 'vue'
import HelloWorld from './components/HelloWorld'
const app = createApp({})
// 全局注冊一個名為hello-wolrd的組件
app.component('hello-wolrd', HelloWorld);
一旦我們?nèi)肿粤私M件,我們就可以在任何地方使用這個組件:<hello-wolrd/>
值得注意的是全局注冊會使Vue失去TypeScript的支持, Vue 3 有一個 PR 擴展了全局組件的接口。目前,Volar 已經(jīng)支持這種用法,我們可以通過在根目錄添加components.d.ts文件的方式來添加全對局組件的TypeScript的支持
declare module 'vue' {
export interface GlobalComponents {
HelloWorld: typeof import('./src/components/HelloWorld.vue')['default']
}
}
局部注冊
我們可以直接從文件中引入vue組件使用,
在單文件組件中(SFC)
<template>
<HelloWorld msg="Welcome to Your Vue.js App"/>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
在JSX中
import HelloWolrd from './components/HelloWorld.vue'
export default {
name: "item",
render(){
return (
<HelloWolrd msg="Welcome to Your Vue.js App"/>
)
}
}
局部注冊的組件在其他組件中無法訪問,在其父組件或子組件或中均不可用,所以你需要在每個使用該組件的地方重新引入并注冊該組件
import HelloWolrd from './components/HelloWorld.vue'
但是這種直接導(dǎo)入組件的方式還有一個好處,如果我們導(dǎo)入的組件使用了typescript,我們無需任何插件就可以在組件中獲得智能提示和類型檢查的功能
局部自動注冊
可以看到局部注冊的優(yōu)點是比較明顯的,但是每次使用我們都需要重復(fù)導(dǎo)入,但是如果你的組件很多,你的組件注冊代碼看起來可能比較冗長,我們可以使用unplugin-vue-components,自動按需導(dǎo)入組件. 它會按需導(dǎo)入組件,但是不再需要導(dǎo)入和組件注冊
該插件會自動對使用的組件生成一個components.d.ts從而獲得TypeScript的支持,
安裝插件
vite
// vite.config.ts
import Components from 'unplugin-vue-components/vite'
export default defineConfig({
plugins: [
Components({ /* options */ }),
],
})
rollup
// rollup.config.js
import Components from 'unplugin-vue-components/rollup'
export default {
plugins: [
Components({ /* options */ }),
],
}
webpack
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-vue-components/webpack')({ /* options */ })
]
}
然后我們可以像往常一樣在模板中使用組件,它會自動進行下面的轉(zhuǎn)換
<template>
<div>
<HelloWorld msg="Hello Vue 3.0 + Vite" />
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
轉(zhuǎn)換成
<template>
<div>
<HelloWorld msg="Hello Vue 3.0 + Vite" />
</div>
</template>
<script>
import HelloWorld from './src/components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
默認它會在src/components目錄下查找組件,我們可以通過dirs參數(shù)來自定義組件目錄,其他配置參考github.com/antfu/unplu…
不同方案對比
| 全局注冊 | 局部注冊 | unplugin-vue-components | |
|---|---|---|---|
| TypeScript支持 | 定義components.d.ts文件 | 默認支持 | 自動生成components.d.ts文件 |
| 組件作用域 | 全局 | 局部 | 局部 |
| 使用方法 | 全局注冊后使用 | 局部注冊后使用 | 添加插件后使用 |
關(guān)于組件名
定義組件名的方式有兩種:
使用 kebab-case:
Vue.component('my-component-name', { /* ... */ })
當(dāng)使用 kebab-case (短橫線分隔命名)定義一個組件時,
你也必須在引用這個自定義元素時使用 kebab-case,例如 <my-component-name>。
使用 駝峰命名法PascalCase
Vue.component('MyComponentName', { /* ... */ })
當(dāng)使用 PascalCase (首字母大寫命名) 定義一個組件時,
你在引用這個自定義元素時兩種命名法都可以使用。
也就是說 <my-component-name> 和 <MyComponentName>都是可接受的。
注意,盡管如此,直接在 DOM (即非字符串的模板) 中使用時只有 kebab-case 是有效的。
所以我們一般建議組件都采用kebab-case這種命名方式。
參考
總結(jié)
到此這篇關(guān)于Vue組件如何自動按需引入的文章就介紹到這了,更多相關(guān)Vue組件自動按需引入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+iview框架實現(xiàn)左側(cè)動態(tài)菜單功能的示例代碼
這篇文章主要介紹了vue+iview框架實現(xiàn)左側(cè)動態(tài)菜單功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Vue 通過自定義指令回顧v-內(nèi)置指令(小結(jié))
這篇文章主要介紹了Vue 通過自定義指令回顧v-內(nèi)置指令(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
前端使用print.js實現(xiàn)打印功能(基于vue)
最近新接了一個需求,想要在前端實現(xiàn)打印功能,下面這篇文章主要給大家介紹了關(guān)于前端使用print.js實現(xiàn)打印功能(基于vue)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
vue動態(tài)加載SVG文件并修改節(jié)點數(shù)據(jù)的操作代碼
這篇文章主要介紹了vue動態(tài)加載SVG文件并修改節(jié)點數(shù)據(jù)的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
vue項目部署到nginx/tomcat服務(wù)器的實現(xiàn)
這篇文章主要介紹了vue項目部署到nginx/tomcat服務(wù)器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Vue-router不允許導(dǎo)航到當(dāng)前位置(/path)錯誤原因以及修復(fù)方式
本文主要介紹了Vue-router不允許導(dǎo)航到當(dāng)前位置(/path)錯誤原因以及修復(fù)方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Vue通過Blob對象實現(xiàn)導(dǎo)出Excel功能示例代碼
這篇文章主要介紹了Vue通過Blob對象實現(xiàn)導(dǎo)出Excel功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

