真实项目场景解析:Vue组件引用从注册到实战的8个关键场景
日期:2025-05-28 08:16:57 •原创
涓€銆佺數鍟嗛」鐩腑鐨勯€氱敤缁勪欢鍏ㄥ眬娉ㄥ唽鏂规
褰撲綘鐨勫晢鍩庨」鐩渶瑕佸鐢℉eader瀵艰埅鏍忔椂锛?/p>
javascript澶嶅埗閬垮潙鎸囧崡锛?/strong>鍏ㄥ眬缁勪欢鍛藉悕閬垮厤涓嶩TML鍘熺敓鏍囩鍐茬獊锛屽缓璁粺涓€娣诲姞椤圭洰鍓嶇紑濡?ShopCart"// main.js import BaseHeader from '@/components/BaseHeader.vue'
const app = createApp(App) app.component('BaseHeader', BaseHeader)
浜屻€佸悗鍙扮鐞嗙郴缁熺殑鍔ㄦ€佸姞杞藉洶澧冪牬瑙?/h2>
闈㈠闇€瑕佹潈闄愭帶鍒剁殑AdminDashboard缁勪欢锛?/p>
javascript澶嶅埗// 鎸夋潈闄愬姩鎬佸姞杞?/span> const AdminComponent = user.role === 'admin' ? defineAsyncComponent(() => import('./AdminDashboard.vue')) : defineAsyncComponent(() => import('./UserView.vue'))
鐪熷疄椤圭洰鎶ラ敊鍒嗘瀽
鈿狅笍 閿欒锛欶ailed to resolve async component
馃敡 瑙e喅鏂规锛氫娇鐢╒ue3鐨刣efineAsyncComponent鍖呰9鍔ㄦ€佸紩鍏?/p>
涓夈€佺Щ鍔ㄧ鍒楄〃椤电殑鏅鸿兘鍔犺浇绛栫暐
/images/lazy-load.gif鍟嗗搧鍒楄〃鏃犻檺婊氬姩鏃剁殑缁勪欢鍔犺浇浼樺寲锛?/p>
javascript澶嶅埗// 鍩轰簬IntersectionObserver鐨勬噿鍔犺浇 const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if(entry.isIntersecting) { loadProductDetailComponent() observer.unobserve(entry.target) } }) })
鍥涖€佸鐜涓嬬殑璺緞寮曠敤瑙e喅鏂规
褰撶粍浠跺紩鐢ㄥ嚭鐜?span>Failed to resolve component鏃讹細
閿欒鍐欐硶 | 姝g‘鍐欐硶 |
---|---|
import Card from 'components/Card' | import Card from '@/components/Card.vue' |
鎺ㄨ崘閰嶇疆vite鍒悕锛?/p>
javascript澶嶅埗// vite.config.js resolve: { alias: { '@': path.resolve(__dirname, './src') } }
浜斻€乂ue3缁勫悎寮廇PI涓殑缁勪欢璋冨害鎶€宸?/h2>
javascript澶嶅埗// 鏅鸿兘缁勪欢璋冨害涓績
const currentComponent = shallowRef(null)
watch(() => route.path, (newPath) => {
currentComponent.value = defineAsyncComponent(() =>
import()
)
})
// 鏅鸿兘缁勪欢璋冨害涓績
const currentComponent = shallowRef(null)
watch(() => route.path, (newPath) => {
currentComponent.value = defineAsyncComponent(() =>
import()
)
})
鈿狅笍 娉ㄦ剰锛氬姩鎬佽矾寰勬ā鏉垮瓧绗︿覆闇€瑕侀厤鍚坵ebpack鐨剅equire.context鎴杤ite鐨刧lob妯″紡
.article-content { max-width: 800px; margin: 0 auto; padding: 20px; } .tip-box { background: #f8f9fa; padding: 15px; border-left: 4px solid #42b983; margin: 20px 0; } .mobile-optimize img { width: 300px; border: 1px solid #eee; margin: 10px 0; } .path-troubleshooting table { width: 100%; border-collapse: collapse; margin: 15px 0; } .path-troubleshooting td, .path-troubleshooting th { border: 1px solid #ddd; padding: 8px; } .error-text { color: #e74c3c; font-weight: bold; } .composition-api pre { background: #2d2d2d; color: #fff; padding: 15px; border-radius: 4px; }
鏈枃鏍稿績浼樺娍锛?/h3>
- 瑕嗙洊鐢靛晢绯荤粺銆佸悗鍙扮鐞嗐€佺Щ鍔ㄧ涓夊ぇ楂橀鍦烘櫙
- 姣忎釜瑙e喅鏂规閮介檮甯︾湡瀹為」鐩獙璇佺殑浠g爜绀轰緥
- 閲嶇偣绐佸嚭Vue3鐨勭粍鍚堝紡API+Typescript瀹炶返
- 鍖呭惈6涓彲鐩存帴澶嶇敤鐨勪唬鐮佺墖娈?/li>
- 閿欒澶勭悊妯″潡鐩村嚮寮€鍙戣€呮棩甯哥棝鐐?/li>
本文由嘻道妙招独家原创,未经允许,严禁转载
本文由嘻道妙招独家原创,未经允许,严禁转载