Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
test-com
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhuxichen
test-com
Commits
9a0328f0
Commit
9a0328f0
authored
Nov 05, 2024
by
zhuxichen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update
parent
6ccef0eb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
3 additions
and
22 deletions
+3
-22
DynamicComponentLoader.vue
src/components/DynamicComponentLoader.vue
+1
-0
TheWelcome.vue
src/components/TheWelcome.vue
+2
-22
No files found.
src/components/DynamicComponentLoader.vue
View file @
9a0328f0
...
...
@@ -42,6 +42,7 @@ const fetchGlobalConfigAndComponentPath = async () => {
if
(
props
.
componentType
===
'local'
)
{
const
res
=
await
fetchGlobalConfig
()
componentPath
=
res
?.
componentKV
?.[
props
.
componentKey
]
// 本地找不到再去远程寻找可用路径 fetchRmoteComponentPath
}
else
{
componentPath
=
await
fetchRmoteComponentPath
()
}
...
...
src/components/TheWelcome.vue
View file @
9a0328f0
<
script
setup
lang=
"ts"
>
import
{
ref
,
onMounted
,
getCurrentInstance
,
defineAsyncComponent
}
from
'vue'
import
{
ref
,
onMounted
,
getCurrentInstance
}
from
'vue'
import
DynamicComponentLoader
from
'./DynamicComponentLoader.vue'
const
instance
=
getCurrentInstance
()
const
DynamicComponent
=
ref
(
null
)
const
globalConfig
=
ref
()
const
getUrl
=
()
=>
{
return
Promise
.
resolve
(
globalConfig
.
value
?.
componentKV
?.
com1
)
}
const
loadComponent
=
async
()
=>
{
try
{
const
componentPath
=
await
getUrl
()
// 获取路径
console
.
log
(
componentPath
)
// 动态导入组件,确保路径使用 Vite 支持的格式
// 对于vite而言,我需要使用绝对路径,如果需要使用alias
// 则需要先import.meta.glob然后再用map[key]的方式
// 但是对于vue.config.js的项目而言,则是可以使用alias
DynamicComponent
.
value
=
defineAsyncComponent
(
()
=>
import
(
/* @vite-ignore */
`
${
componentPath
}
`
),
)
}
catch
(
error
)
{
console
.
error
(
'Error loading component:'
,
error
)
}
}
onMounted
(()
=>
{
globalConfig
.
value
=
instance
?.
appContext
.
config
.
globalProperties
.
$globalConfig
as
Record
<
string
,
string
|
object
>
loadComponent
()
})
</
script
>
<
template
>
<div>
{{
$
globalConfig
?.
name
}}
</div>
<div>
{{
globalConfig
?.
name
}}
</div>
<!--
<component
:is=
"DynamicComponent"
v-if=
"DynamicComponent"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment