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
cd49756c
Commit
cd49756c
authored
Nov 05, 2024
by
zhuxichen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
demo 支持从远程获取组件,检测mode为remote时减少getConfig消耗
parent
3d5fd1d1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
23 deletions
+46
-23
env.d.ts
env.d.ts
+1
-1
DynamicComponentLoader.vue
src/components/DynamicComponentLoader.vue
+28
-22
useConfig.ts
src/config/useConfig.ts
+17
-0
No files found.
env.d.ts
View file @
cd49756c
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
import
{
ComponentCustomProperties
}
from
'vue'
import
{
ComponentCustomProperties
}
from
'vue'
// 定义 $globalConfig 的接口
// 定义 $globalConfig 的接口
interface
GlobalConfig
{
export
interface
GlobalConfig
{
componentKV
:
Record
<
string
,
string
>
componentKV
:
Record
<
string
,
string
>
[
v
:
string
]:
string
|
object
[
v
:
string
]:
string
|
object
}
}
...
...
src/components/DynamicComponentLoader.vue
View file @
cd49756c
...
@@ -8,22 +8,23 @@
...
@@ -8,22 +8,23 @@
</
template
>
</
template
>
<
script
setup
lang=
"ts"
>
<
script
setup
lang=
"ts"
>
import
{
import
{
useConfig
}
from
'@/config/useConfig'
ref
,
import
{
ref
,
onMounted
,
defineAsyncComponent
,
defineProps
}
from
'vue'
onMounted
,
defineAsyncComponent
,
interface
Props
{
defineProps
,
getCurrentInstance
,
}
from
'vue'
const
props
=
defineProps
<
{
componentKey
:
string
// 组件键
componentKey
:
string
// 组件键
}
>
()
// local请求本地的key remote请求接口的key
componentType
:
'local'
|
'remote'
}
const
props
=
withDefaults
(
defineProps
<
Props
>
(),
{
componentType
:
'local'
,
})
const
instance
=
getCurrentInstance
()
const
globalConfig
=
ref
<
Record
<
string
,
string
|
object
>
|
undefined
>
(
undefined
)
const
DynamicComponent
=
ref
(
null
)
const
DynamicComponent
=
ref
(
null
)
const
{
fetchGlobalConfig
}
=
useConfig
()
// 动态导入组件
// 动态导入组件
const
loadComponent
=
async
(
componentPath
:
string
)
=>
{
const
loadComponent
=
async
(
componentPath
:
string
)
=>
{
DynamicComponent
.
value
=
defineAsyncComponent
(
DynamicComponent
.
value
=
defineAsyncComponent
(
...
@@ -31,20 +32,25 @@ const loadComponent = async (componentPath: string) => {
...
@@ -31,20 +32,25 @@ const loadComponent = async (componentPath: string) => {
)
)
}
}
// 获取全局配置和动态组件路径
const
fetchRmoteComponentPath
=
async
()
=>
{
return
Promise
.
resolve
(
'/src/components/testComponent/TestCom1.vue'
)
}
// 区分组件类型,以获取组件路径【全局 / 远程】
const
fetchGlobalConfigAndComponentPath
=
async
()
=>
{
const
fetchGlobalConfigAndComponentPath
=
async
()
=>
{
globalConfig
.
value
=
let
componentPath
:
string
|
undefined
instance
?.
appContext
.
config
.
globalProperties
.
$globalConfig
if
(
props
.
componentType
===
'local'
)
{
const
componentKV
=
(
globalConfig
.
value
?.
componentKV
||
{})
as
Record
<
const
res
=
await
fetchGlobalConfig
()
string
,
componentPath
=
res
?.
componentKV
?.[
props
.
componentKey
]
string
}
else
{
>
componentPath
=
await
fetchRmoteComponentPath
()
// 可以是一个await 来接受componentPath
}
const
componentPath
=
componentKV
?.[
props
.
componentKey
]
if
(
componentPath
)
{
if
(
componentPath
)
{
loadComponent
(
componentPath
)
loadComponent
(
componentPath
)
}
else
{
}
else
{
console
.
warn
(
`Component path not found for key:
${
props
.
componentKey
}
`
)
console
.
warn
(
`Component path with
${
props
.
componentType
}
not found for key:
${
props
.
componentKey
}
`
,
)
}
}
}
}
...
...
src/config/useConfig.ts
0 → 100644
View file @
cd49756c
import
type
{
GlobalConfig
}
from
'env'
import
{
ref
,
getCurrentInstance
}
from
'vue'
export
const
useConfig
=
()
=>
{
const
instance
=
getCurrentInstance
()
const
globalConfig
=
ref
<
GlobalConfig
>
()
const
fetchGlobalConfig
=
()
=>
{
if
(
instance
)
{
globalConfig
.
value
=
instance
.
appContext
.
config
.
globalProperties
.
$globalConfig
}
return
Promise
.
resolve
(
globalConfig
.
value
)
}
return
{
globalConfig
,
fetchGlobalConfig
}
}
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