mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat: add useRoute
This commit is contained in:
parent
9f372af1ec
commit
762aed3e5d
@ -1,6 +1,6 @@
|
|||||||
import { computed } from 'vue';
|
import { computed } from 'vue';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { route, routeProps } from '../utils/router';
|
import { useRoute, routeProps } from '../utils/router';
|
||||||
import { useParent } from '../api/use-relation';
|
import { useParent } from '../api/use-relation';
|
||||||
import { ACTION_BAR_KEY } from '../action-bar';
|
import { ACTION_BAR_KEY } from '../action-bar';
|
||||||
import Button from '../button';
|
import Button from '../button';
|
||||||
@ -19,6 +19,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
|
const route = useRoute();
|
||||||
const { parent, index } = useParent(ACTION_BAR_KEY, { isButton: true });
|
const { parent, index } = useParent(ACTION_BAR_KEY, { isButton: true });
|
||||||
|
|
||||||
const isFirst = computed(() => {
|
const isFirst = computed(() => {
|
||||||
@ -37,13 +38,9 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return (vm) => {
|
return () => {
|
||||||
const { type, icon, text, color, loading, disabled } = props;
|
const { type, icon, text, color, loading, disabled } = props;
|
||||||
|
|
||||||
const onClick = () => {
|
|
||||||
route(vm.$router, vm);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
class={bem([
|
class={bem([
|
||||||
@ -59,7 +56,7 @@ export default createComponent({
|
|||||||
color={color}
|
color={color}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onClick={onClick}
|
onClick={route}
|
||||||
>
|
>
|
||||||
{slots.default ? slots.default() : text}
|
{slots.default ? slots.default() : text}
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { route, routeProps } from '../utils/router';
|
import { useRoute, routeProps } from '../utils/router';
|
||||||
import { useParent } from '../api/use-relation';
|
import { useParent } from '../api/use-relation';
|
||||||
import { ACTION_BAR_KEY } from '../action-bar';
|
import { ACTION_BAR_KEY } from '../action-bar';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
@ -19,6 +19,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
|
const route = useRoute();
|
||||||
useParent(ACTION_BAR_KEY);
|
useParent(ACTION_BAR_KEY);
|
||||||
|
|
||||||
const renderIcon = () => {
|
const renderIcon = () => {
|
||||||
@ -45,15 +46,8 @@ export default createComponent({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (vm) => (
|
return () => (
|
||||||
<div
|
<div role="button" class={bem()} tabindex="0" onClick={route}>
|
||||||
role="button"
|
|
||||||
class={bem()}
|
|
||||||
tabindex="0"
|
|
||||||
onClick={() => {
|
|
||||||
route(vm.$router, vm);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{renderIcon()}
|
{renderIcon()}
|
||||||
{slots.default ? slots.default() : props.text}
|
{slots.default ? slots.default() : props.text}
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// Utils
|
// Utils
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { BORDER_SURROUND, WHITE } from '../utils/constant';
|
import { BORDER_SURROUND, WHITE } from '../utils/constant';
|
||||||
import { routeProps, route } from '../utils/router';
|
import { useRoute, routeProps } from '../utils/router';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
@ -47,6 +47,8 @@ export default createComponent({
|
|||||||
emits: ['click'],
|
emits: ['click'],
|
||||||
|
|
||||||
setup(props, { emit, slots }) {
|
setup(props, { emit, slots }) {
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
const renderLoadingIcon = () => {
|
const renderLoadingIcon = () => {
|
||||||
if (slots.loading) {
|
if (slots.loading) {
|
||||||
return slots.loading();
|
return slots.loading();
|
||||||
@ -114,7 +116,7 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (vm) => {
|
return () => {
|
||||||
const {
|
const {
|
||||||
tag,
|
tag,
|
||||||
type,
|
type,
|
||||||
@ -132,7 +134,7 @@ export default createComponent({
|
|||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
if (!loading && !disabled) {
|
if (!loading && !disabled) {
|
||||||
emit('click', event);
|
emit('click', event);
|
||||||
route(vm.$router, vm);
|
route();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// Utils
|
// Utils
|
||||||
import { createNamespace, isDef } from '../utils';
|
import { createNamespace, isDef } from '../utils';
|
||||||
import { route, routeProps } from '../utils/router';
|
import { useRoute, routeProps } from '../utils/router';
|
||||||
import { cellProps } from './shared';
|
import { cellProps } from './shared';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
@ -15,6 +15,8 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { slots }) {
|
setup(props, { slots }) {
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
const renderLabel = () => {
|
const renderLabel = () => {
|
||||||
const showLabel = slots.label || isDef(props.label);
|
const showLabel = slots.label || isDef(props.label);
|
||||||
|
|
||||||
@ -83,7 +85,7 @@ export default createComponent({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (vm) => {
|
return () => {
|
||||||
const { size, center, border, isLink, required } = props;
|
const { size, center, border, isLink, required } = props;
|
||||||
const clickable = isLink || props.clickable;
|
const clickable = isLink || props.clickable;
|
||||||
|
|
||||||
@ -97,16 +99,12 @@ export default createComponent({
|
|||||||
classes[size] = size;
|
classes[size] = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
const onClick = () => {
|
|
||||||
route(vm.$router, vm);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
class={bem(classes)}
|
class={bem(classes)}
|
||||||
role={clickable ? 'button' : null}
|
role={clickable ? 'button' : null}
|
||||||
tabindex={clickable ? 0 : null}
|
tabindex={clickable ? 0 : null}
|
||||||
onClick={onClick}
|
onClick={route}
|
||||||
>
|
>
|
||||||
{renderLeftIcon()}
|
{renderLeftIcon()}
|
||||||
{renderTitle()}
|
{renderTitle()}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { route, routeProps } from '../utils/router';
|
import { useRoute, routeProps } from '../utils/router';
|
||||||
import { useParent } from '../api/use-relation';
|
import { useParent } from '../api/use-relation';
|
||||||
import { SIDEBAR_KEY } from '../sidebar';
|
import { SIDEBAR_KEY } from '../sidebar';
|
||||||
import Badge from '../badge';
|
import Badge from '../badge';
|
||||||
@ -18,9 +18,10 @@ export default createComponent({
|
|||||||
emits: ['click'],
|
emits: ['click'],
|
||||||
|
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
|
const route = useRoute();
|
||||||
const { parent, index } = useParent(SIDEBAR_KEY);
|
const { parent, index } = useParent(SIDEBAR_KEY);
|
||||||
|
|
||||||
return (vm) => {
|
return () => {
|
||||||
const { dot, badge, title, disabled } = props;
|
const { dot, badge, title, disabled } = props;
|
||||||
const selected = index.value === parent.active();
|
const selected = index.value === parent.active();
|
||||||
|
|
||||||
@ -32,7 +33,7 @@ export default createComponent({
|
|||||||
emit('click', index.value);
|
emit('click', index.value);
|
||||||
parent.emit('update:modelValue', index.value);
|
parent.emit('update:modelValue', index.value);
|
||||||
parent.setActive(index.value);
|
parent.setActive(index.value);
|
||||||
route(vm.$router, vm);
|
route();
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Vue Router support
|
* Vue Router support
|
||||||
*/
|
*/
|
||||||
|
import { getCurrentInstance } from 'vue';
|
||||||
import type { Router, RouteLocation } from 'vue-router';
|
import type { Router, RouteLocation } from 'vue-router';
|
||||||
|
|
||||||
export type RouteConfig = {
|
export type RouteConfig = {
|
||||||
@ -18,8 +18,8 @@ function isRedundantNavigation(err: Error) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function route(router: Router, config: RouteConfig) {
|
export function route(router: Router, props: RouteProps) {
|
||||||
const { to, url, replace } = config;
|
const { to, url, replace } = props;
|
||||||
if (to && router) {
|
if (to && router) {
|
||||||
const promise = router[replace ? 'replace' : 'push'](to);
|
const promise = router[replace ? 'replace' : 'push'](to);
|
||||||
|
|
||||||
@ -36,6 +36,13 @@ export function route(router: Router, config: RouteConfig) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useRoute() {
|
||||||
|
const vm = getCurrentInstance()!.proxy!;
|
||||||
|
return () => {
|
||||||
|
route(vm.$router, vm as RouteProps);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export type RouteProps = {
|
export type RouteProps = {
|
||||||
url?: string;
|
url?: string;
|
||||||
replace?: boolean;
|
replace?: boolean;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user