gxlmyacc / react-view-router Goto Github PK
View Code? Open in Web Editor NEWwrite route config like vue-router in react
License: MIT License
write route config like vue-router in react
License: MIT License
// / routes.js
import { normalizeRoutes, lazyImport } from 'react-view-router';
import Home from '@/layout';
const routes = normalizeRoutes([
{
path: '/home',
component: Home,
children: [
{ path: '/', redirect: 'main' },
{
path: 'main',
// 懒加载
component: lazyImport(() => import('@/view/pulmonary')),
},
],
},
// {
// path: '/login',
// component: Login
// }
]);
export default routes;
首先ts提示类型不正确
不能将类型“RouteLazy”分配给类型“ComponentType<{}> | undefined”。
不能将类型“RouteLazy”分配给类型“FunctionComponent<{}>”。
类型“RouteLazy”提供的内容与签名“(props: { children?: ReactNode; }, context?: any): ReactElement<any, any> | null”不匹配。
改造成下面形式
components: {
default: lazyImport(() => import('@/view/pulmonary')),
},
浏览器抛出error
React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object
''
给path设置为''
会自动转化为/
Hello @gxlmyacc , I am trying to use this awesome routing tool, however, I am getting this error in the browser console:
Uncaught TypeError: Cannot read properties of undefined (reading 'default')
at Module.get [as RouterView] (Middleware.js:101)
at getExportByName (modules.js?hash=2d5fd69c588994d60e8131b827f3ffc3ec129d1f:1001)
at modules.js?hash=2d5fd69c588994d60e8131b827f3ffc3ec129d1f:962
at Array.forEach (<anonymous>)
at forEachSetter (modules.js?hash=2d5fd69c588994d60e8131b827f3ffc3ec129d1f:945)
at Entry.Ep.runSetters (modules.js?hash=2d5fd69c588994d60e8131b827f3ffc3ec129d1f:809)
at Module.runSetters (modules.js?hash=2d5fd69c588994d60e8131b827f3ffc3ec129d1f:377)
at fileEvaluate (modules-runtime-hot.js?hash=d12281259a6cee99786429a66f76b05f13449c7f:404)
at Module.require (modules-runtime-hot.js?hash=d12281259a6cee99786429a66f76b05f13449c7f:270)
at require (modules-runtime-hot.js?hash=d12281259a6cee99786429a66f76b05f13449c7f:310)
Any idea to solve it?
I am using:
Hi @gxlmyacc , I am trying to use this plugin with react-view-router
to inject transitions when a route is changed. The plugin is inspired by the vue transition modes and it was designed to work with React Router, so, I guess that it could work as well with react-view-router
since this router is based on React Router. An example with React Router is the following code:
<Route key={path} exact path={path}>
{({ match }) => (
<CSSTransition
in={match != null}
timeout={300}
classNames="page"
unmountOnExit
>
<div className="page">
<Component />
</div>
</CSSTransition>
)}
</Route>
So, with react-view-router I have this code:
<CSSTransition in={false} // "in" prop should have a condition as value.
timeout={300} classNames="sectionView" unmountOnExit>
<div className="sectionView">
<RouterView name="sectionView" />
</div>
</CSSTransition>
So, I'm still figuring out where to take the match
variable to inject the condition match !== null
.
You can see the full example with React Router here.
Is there a way to achieve it?
Hello, I started to implement this routing tool in a react app, so, I am trying to replicate the routing configuration from another Vue app that I have made, however, I am having an issue with nested routes. To make this more understandable, this is my routing configuration:
routes.ts
import { normalizeRoutes } from 'react-view-router';
import LayoutSPA from '/imports/ui/layouts/LayoutSPA';
import LayoutAuth from '/imports/ui/layouts/LayoutAuth';
import Login from '/imports/ui/views/Auth/Login';
import ForgotPassword from '/imports/ui/views/Auth/ForgotPassword';
export default normalizeRoutes([
{ path: '/', index: 'home' },
{
path: '/home',
name: 'home',
component: LayoutSPA
},
{
path: '/login',
component: LayoutAuth,
children: [
{
name: 'forgotPassword',
path: '/forgot-password',
components: {
sectionView: ForgotPassword
}
},
{
name: 'login',
path: '',
components: {
sectionView: Login
}
}
]
}
]);
In Vue 2 apps, I can use nested views with root paths, however, in the React app, if I want to access to ForgotPassword view, I have to put the /login/forgot-password path, whereas in Vue you can put only /forgot-password and it will work because the pathname for ForgotPassword view starts with a slash ( / ). So, I was wondering if this can be possible somehow?
On the other hand, I have 2 questions more:
What is the difference between index and redirect fields of a route?
What is the name property used for? (I use it in Vue apps to identify the route and it was used to navigate between routes, as an alternative field from the path field)
Hope you can help me with this :) .
Do you plan ssr support (next.js)?
After installing and running, the console throws an error
Uncaught ReferenceError: global is not defined
<anonymous> global.ts:5
It looks like global.ts references global
and it is not defined. Maybe you meant to use globalThis
?
Thanks
Could not find dependency: 'core-js' relative to '/node_modules/react-view-router/esm/index.js'
after build scripts go to esm folder, but code in es folder
Hi, thanks for router like vuejs, its good router API
how to use with SSR?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.