Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pro-layout
Project
Project
Details
Activity
Releases
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
packages
pro-layout
Commits
4c4eab5e
Commit
4c4eab5e
authored
Oct 22, 2020
by
Sendya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: readonly createContext
parent
c1f17791
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
41 deletions
+12
-41
route-context.tsx
examples/route-context.tsx
+2
-7
index.ts
src/hooks/context/index.ts
+10
-34
No files found.
examples/route-context.tsx
View file @
4c4eab5e
...
@@ -6,14 +6,13 @@ import 'ant-design-vue/dist/antd.less';
...
@@ -6,14 +6,13 @@ import 'ant-design-vue/dist/antd.less';
const
DemoComponent
=
{
const
DemoComponent
=
{
setup
()
{
setup
()
{
const
jsonEditorRef
=
ref
(
null
);
const
state
=
reactive
({
const
state
=
reactive
({
name
:
'value'
,
name
:
'value'
,
});
});
const
{
const
{
state
:
routeContext
,
state
:
routeContext
,
p
rovider
:
RouteContextProvider
P
rovider
:
RouteContextProvider
}
=
createRouteContext
({
}
=
createRouteContext
({
hasSideMenu
:
true
,
hasSideMenu
:
true
,
collapsed
:
true
,
collapsed
:
true
,
...
@@ -21,10 +20,6 @@ const DemoComponent = {
...
@@ -21,10 +20,6 @@ const DemoComponent = {
menuData
:
[]
menuData
:
[]
});
});
onMounted
(()
=>
{
console
.
log
(
'jsonEditorRef'
,
jsonEditorRef
)
})
return
()
=>
(
return
()
=>
(
<
div
class=
"components"
>
<
div
class=
"components"
>
<
h2
>
# Template
</
h2
>
<
h2
>
# Template
</
h2
>
...
@@ -66,7 +61,7 @@ const DemoComponent = {
...
@@ -66,7 +61,7 @@ const DemoComponent = {
const
TestChildComponent
=
defineComponent
({
const
TestChildComponent
=
defineComponent
({
setup
()
{
setup
()
{
const
routeContext
=
useRouteContext
();
const
routeContext
=
useRouteContext
();
console
.
log
(
'TestChildComponent.routeContext'
,
routeContext
)
console
.
log
(
'TestChildComponent.routeContext'
,
routeContext
)
;
return
()
=>
{
return
()
=>
{
const
{
menuData
,
collapsed
}
=
routeContext
const
{
menuData
,
collapsed
}
=
routeContext
...
...
src/hooks/context/index.ts
View file @
4c4eab5e
import
{
import
{
defineComponent
,
defineComponent
,
h
,
InjectionKey
,
InjectionKey
,
provide
,
provide
,
inject
,
inject
,
reactive
,
reactive
,
RendererElement
,
readonly
,
RendererNode
,
SetupContext
,
SetupContext
,
toRefs
,
UnwrapRef
,
UnwrapRef
,
VNode
,
VNode
,
PropType
,
DefineComponent
,
DefineComponent
,
}
from
'vue'
;
}
from
'vue'
;
export
type
ContextType
<
T
>
=
any
;
export
type
ContextType
<
T
>
=
any
;
export
interface
CreateContext
<
T
>
{
export
interface
CreateContext
<
T
>
{
provider
:
DefineComponent
<
{},
()
=>
VNode
|
VNode
[]
>
;
Provider
:
DefineComponent
<
{},
()
=>
VNode
|
VNode
[]
|
undefined
,
any
>
;
state
:
UnwrapRef
<
T
>
|
T
;
state
:
UnwrapRef
<
T
>
|
T
;
}
}
const
RouteContextProvider
=
defineComponent
({
name
:
'RouteContextProvider'
,
inheritAttrs
:
false
,
props
:
{
contextInjectKey
:
{
type
:
[
Object
,
String
,
Symbol
]
as
PropType
<
InjectionKey
<
any
>>
,
required
:
true
,
},
},
setup
(
props
,
{
slots
,
attrs
}:
SetupContext
)
{
console
.
log
(
'props'
,
props
,
attrs
);
const
context
=
reactive
({
...
attrs
,
});
provide
(
props
.
contextInjectKey
,
context
);
return
()
=>
slots
.
default
();
},
});
export
const
createContext
=
<
T
>
(
context
:
ContextType
<
T
>
,
export
const
createContext
=
<
T
>
(
context
:
ContextType
<
T
>
,
contextInjectKey
:
InjectionKey
<
ContextType
<
T
>>
=
Symbol
()
contextInjectKey
:
InjectionKey
<
ContextType
<
T
>>
=
Symbol
()
,
):
CreateContext
<
T
>
=>
{
):
CreateContext
<
T
>
=>
{
const
state
=
reactive
<
ContextType
<
T
>>
({
const
state
=
reactive
<
ContextType
<
T
>>
({
...
context
,
...
context
,
});
});
const
ContextProvider
=
defineComponent
(
{
const
ContextProvider
=
defineComponent
({
name
:
'ContextProvider'
,
name
:
'ContextProvider'
,
inheritAttrs
:
false
,
inheritAttrs
:
false
,
setup
(
props
,
{
slots
}:
SetupContext
)
{
setup
(
props
,
{
slots
}:
SetupContext
)
{
provide
(
contextInjectKey
,
state
);
provide
(
contextInjectKey
,
readonly
(
state
)
);
return
()
=>
slots
.
default
();
return
()
=>
slots
.
default
();
},
},
})
})
;
return
{
return
{
state
,
state
,
p
rovider
:
ContextProvider
,
P
rovider
:
ContextProvider
,
};
};
};
};
export
const
useContext
=
<
T
>
(
contextInjectKey
:
InjectionKey
<
ContextType
<
T
>>
=
Symbol
(),
defaultValue
?:
ContextType
<
T
>
):
T
=>
{
export
const
useContext
=
<
T
>
(
contextInjectKey
:
InjectionKey
<
ContextType
<
T
>>
=
Symbol
(),
defaultValue
?:
ContextType
<
T
>
):
T
=>
{
return
inject
(
contextInjectKey
,
defaultValue
||
{}
as
T
)
return
readonly
(
inject
(
contextInjectKey
,
defaultValue
||
{}
as
T
));
}
}
;
// :: examples ::
// :: examples ::
//
//
...
...
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