我有两个文件,我用react.lazy 和 suspense 加载:
import React, { Suspense, lazy } from "react";
import { Route, Redirect } from 'react-router-dom';
const MainLayout = lazy(() => import('Components/Layout/MainLayout'))
export const PrivateRoute = () => (
<Route render={() => {
return (
localStorage.getItem('user') != null// validation that it is a valid user
?
<Suspense fallback={<div>Loading...</div>}>
<MainLayout/>
</Suspense>
: <Redirect to={{ pathname: '/login'}} />)
}} />
)
Run Code Online (Sandbox Code Playgroud)
第二:
import React, { Suspense, lazy } from "react";
const DefaultLayout = lazy(() => import('Components/Layout/DefaultLayout'))
export const PublicRoute …Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的表:
CREATE TABLE IF NOT EXISTS
newbook_mast (book_id varchar(15) NOT NULL UNIQUE,
book_name varchar(50) ,
isbn_no varchar(15) NOT NULL UNIQUE ,
cate_id varchar(8) ,
aut_id varchar(8) ,
pub_id varchar(8) ,
dt_of_pub date ,
pub_lang varchar(15) ,
no_page decimal(5,0)
book_price decimal(8,2) ,
PRIMARY KEY (book_id)
);
Run Code Online (Sandbox Code Playgroud)
如果我想在列上添加检查,我只需写:
CHECK(no_page>0)
Run Code Online (Sandbox Code Playgroud)
但我没有编写代码,而是在 mysql 工作台中创建图表模型。我无法找到如何仅使用图表模型来添加如上所述的检查?
是否可以?
尝试使用反应,我决定测试打字稿。
代码:
import { BrowserRouter } from 'react-router-dom'
import history from './utilities/history'
ReactDOM.render(
<BrowserRouter history={history}>
<App />
</BrowserRouter>,
document.getElementById('root')
)
Run Code Online (Sandbox Code Playgroud)
历史.js:
import { createBrowserHistory } from 'history'
export default createBrowserHistory()
Run Code Online (Sandbox Code Playgroud)
错误:
类型 '{ 子元素:元素;历史:历史;}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly & Readonly<{children?: ReactNode; }>'。类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly & Readonly<{children?: ReactNode;”上不存在属性“history” }>'.ts(2322)
包.json:
@types/react-router-dom": "^4.3.4",
react-router-dom": "^5.0.1",
Run Code Online (Sandbox Code Playgroud)
当没有打字稿做完全相同的事情时,代码就可以工作。我不明白为什么会发生这种情况,有人有答案吗?
我有一个用户表,其中有一个角色表的外键列。
我在 mysql 中定义了所有关系,并使用sequelize-auto我生成了模型。
为用户生成的模型是这样的:
const user = sequelize.define('user', {
Id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
Email: {
type: DataTypes.STRING(45),
allowNull: false,
unique: true,
},
RoleId: {
type: DataTypes.INTEGER(11),
allowNull: false,
references: {
model: 'roles',
key: 'Id',
},
},
});
Run Code Online (Sandbox Code Playgroud)
我认为我的参考已设置,因此当我在解析器中执行以下操作时:
users: async () => {
const users = await db.user.findAll({
include: [
{
model: db.roles,
},
],
});
return users
Run Code Online (Sandbox Code Playgroud)
我应该在操场上使用以下查询返回用户角色列表:
{users
{roles
{Name, Id}
}
}
Run Code Online (Sandbox Code Playgroud)
相反我得到了
角色未与用户关联!
后来我发现我需要建立一个关联:
user.associate = …Run Code Online (Sandbox Code Playgroud) 这有点类似于这个问题:
但就我而言,我正在加载这样的脚本:
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','ID');</script>
<!-- End Google Tag Manager -->
Run Code Online (Sandbox Code Playgroud)
现在我知道有一个用于 google 标签管理器的 npm 包,但我很好奇我是否想以自定义方式执行此操作,我该怎么做?
在上面的问题中,我看到了很多:
const script = document.createElement("script");
script.src = "https://use.typekit.net/foobar.js";
script.async = true;
document.body.appendChild(script);
Run Code Online (Sandbox Code Playgroud)
这很好,但如果我在加载的脚本中有一个函数,我将如何正确执行它?
我需要帮助解释一段代码。
我一直在尝试学习如何使用 jwt 对用户进行身份验证并返回令牌和刷新令牌。
这是一次有趣的旅程,我遇到了这个 存储库,其中用户添加了一个扩展/链函数,据我所知,它结合了多个 graphql 解析器:
// I don't understand this function
const createResolver = (resolver) => {
const baseResolver = resolver;
baseResolver.createResolver = (childResolver) => {
const newResolver = async (parent, args, context, info) => {
await resolver(parent, args, context, info);
return childResolver(parent, args, context, info);
};
return createResolver(newResolver);
};
return baseResolver;
};
export const requiresAuth = createResolver((parent, args, context) => {
if (!context.user || !context.user.id) {
throw new Error('Not authenticated');
}
});
export const requiresAdmin …Run Code Online (Sandbox Code Playgroud) 我重新访问了我的数据库并注意到我有一些 INT 类型的主键。
这还不够独特,所以我想我会有一个指导。我来自 microsoft sql 背景,在 ssms 中,您可以选择类型为“uniqeidentifier”并自动增加它。
但是,在 mysql 中,我发现您必须为要为其生成指南 ID 的表创建在插入时执行的触发器。例子:
桌子:
CREATE TABLE `tbl_test` (
`GUID` char(40) NOT NULL,
`Name` varchar(50) NOT NULL,
PRIMARY KEY (`GUID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)
扳机:
CREATE TRIGGER `t_GUID` BEFORE INSERT ON `tbl_test`
FOR EACH ROW begin
SET new.GUID = uuid();
Run Code Online (Sandbox Code Playgroud)
或者,您必须自己在后端插入 guid。
我不是数据库专家,但仍然记得触发器会导致性能问题。
我有这个:
public class RuleController : ApiController
{
private readonly IRuleManager _ruleManager;
// GET: api/Rule/guid
[HttpGet]
public IHttpActionResult GetRule(Guid id)
{
var rules = _ruleManager.GetRulesById(id);
return Ok(rules);
}
[HttpGet]
public async Task<IHttpActionResult> GetRuleNew(string locale, string pno, string modelYear)
{
// do cool stuff with my params
}
}
Run Code Online (Sandbox Code Playgroud)
我的路线配置:
config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
config.Routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
config.Routes.MapHttpRoute("DefaultApiPost", …Run Code Online (Sandbox Code Playgroud) 今天我和一位同事争论关于使用returna 中的“”键useEffect来停止代码执行。我了解到return只能在清理时使用。
问题:
useEffect(() => {
if (stateCondition) {
//do fancy stuff
return;
}
// more code to execute if condition is not met
}, [props]);
Run Code Online (Sandbox Code Playgroud)
我的看法是,我们也许应该有另一个useEffect仅在状态为 false 时才执行的方法,而不是使用return上面的“”。
我试图找到一些文件来支持我的主张,但到目前为止我还没有找到。
我在寻找什么:
我在这儿吗?是否有文件支持我的主张或其他方式?
javascript ×6
reactjs ×4
graphql ×2
mysql ×2
.net ×1
c# ×1
create-table ×1
css ×1
diagram ×1
primary-key ×1
react-hooks ×1
react-router ×1
routes ×1
sequelize.js ×1
sql ×1
typescript ×1
uid ×1