在定义valuefor onClick属性时,我们知道如下语法:
<button type="submit" onclick="alert('hi');"></button>
<button type="submit" onclick="doWork"></button> <!-- This one doesn't work -->
<button type="submit" onclick="doWork()"></button>
<button type="submit" onclick="doWork('Mike', 2)"></button>
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是定义一个自定义data-attribute并执行如下值:
<button type="submit" data-callback="alert('hi');" class="marker"></button>
<button type="submit" data-callback="doWork" class="marker"></button>
<button type="submit" data-callback="doWork()" class="marker"></button>
<button type="submit" data-callback="doWork('Mike', 2)" class="marker"></button>
<script type="text/javascript">
jQuery("body").on("click","button.marker", function(e) {
var callback = jQuery(e.currentTarget).data("callback");
// Now I need to execute the callback no matter of the format
// 1. Execute as function's body
// 2. Or by function 'name'
// 3. …Run Code Online (Sandbox Code Playgroud) 您好,根据文档getServerSideProps是一个仅在服务器端执行的函数。
export async function getServerSideProps(context) {
console.log("hello world");
debugger;
return {
props: {
age:55
}, // will be passed to the page component as props
}
}
Run Code Online (Sandbox Code Playgroud)
因此,简单地删除调试器关键字是行不通的。
我努力了:
node --inspect-brk ./node_modules/next/dist/bin/nextcross-env NODE_OPTIONS='--inspect' next dev但console.log()和debugger关键字似乎都没有任何效果。
然而,我来自 getServerSideProps 的道具被正确传递,这证明该函数被调用。
是否可以单步执行 getServerSideProps 或至少让 console.log 工作?谢谢!
聚苯乙烯
getServerSideProps 外部的代码行是可调试的并且可以按预期工作。
我正在努力描绘 azure 管道的文件夹结构。我知道有一些隐式目录,例如:
这两个文件夹都是池中可用的特定构建代理上的文件夹。
有没有办法查看文件夹结构并更好地了解事物的布局?
我在使用 TypeScript 输入对象时遇到问题
我声明了应用程序的某些文件使用的类型:
type Category = "cat1"|"cat2"|"cat3"|"cat4"
Run Code Online (Sandbox Code Playgroud)
我用来Category输入一个对象并且一切正常使用:
const obj: {
[key in Category]: string
} = {---}
Run Code Online (Sandbox Code Playgroud)
但现在,我想添加 2 个不是Category对象值的键。我认为通过输入这样的对象会很容易:
const obj: {
customKey1: string
customKey2: string
[key in Category]: string
} = {---}
Run Code Online (Sandbox Code Playgroud)
但 TS 没有按预期工作,而是发送了 3 个错误:TS2464、TS1170 和 TS2693
A computed property name must be of type 'string', 'number', 'symbol' or 'any'. ts(2464)
'Category' only refers to a type, but is using as a value here. ts(2693)
A computed property name in …Run Code Online (Sandbox Code Playgroud) 我正在尝试为反应路由器4匹配正则表达式模式,但不幸的this.props.match.params.id是,它无法正确解析路径,并显示为未定义。
我希望能够访问的路径示例:
我的组件:
import React from "react";
import PropTypes from "prop-types";
import { withRouter, } from "react-router";
import { Switch, Route } from "react-router-dom";
class Foo extends React.Component {
render(){
console.log(this.props.match);
return <div>Match: {this.props.match.params.id}</div>
}
}
const industryRegex = "(air|motor|ship)";
const guidRegex = "([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})?"; // Note the questionmark ? at the end
const pathId = `/gps/:id${guidRegex}`;
const pathIndustry = `/gps/:industryType${industryRegex}`;
const pathIndustryAndId = `/gps/:industryType${industryRegex}/:id${guidRegex}`; …Run Code Online (Sandbox Code Playgroud) 假设我有这样的类型定义:
name必须具有或fullname已定义财产的人
type Person = {
[k in "name" | "fullname"]: string;
};
Run Code Online (Sandbox Code Playgroud)
假设我想再添加一个必需的属性age,直观上我会写这样的内容:
type Person = {
[k in "name" | "fullname"]: string;
age: number; // This errors
};
Run Code Online (Sandbox Code Playgroud)
然而,这种语法不起作用,唯一的方法是使用交集运算符,&如下所示:
type Person = {
[k in "name" | "fullname"]: string;
} & { age: number };
Run Code Online (Sandbox Code Playgroud)
两个问题:
mapStateToProps在构造函数之前调用?作为1的副作用
constructor (props) {
base(props)
// props already have values from "mapStateToTprops"
}
为什么要自动完成?
mapStateToProps调用ComponentWillReceiveProps(这是第一次加载时的情况)请参阅此链接在此处输入链接描述如果我想写一个像这样的条件:
if (props.isAuthenticated) {
browserHistory.push("/admin/dashboard")
}
哪种方法最适合挂钩.请记住,我想在每次状态更改时强制执行此条件(因为根据leo的回答,ComponentWillReceiveProps不可靠)?
我无法获得触发OnSuccess()方法的元素的目标Ajax.BeginForm().所以这是代码片段:
@using (Ajax.BeginForm("InsertModel", "Home", new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "doWork(this,'SomeCustomText')"
}))
{
<div id="container">
<--Some HTML--!>
<input type="submit" value="OK"/>
</div>
}
<script>
function doWork(e,customText)
{
alert(customText); //It shows 'SomeCustomText', so its good.
alert(e); //[Object] object
alert(e.prop("tagName")); //Object #<Object> has no method 'prop'
alert(e.attr("tagName")); //Object #<Object> has no method 'attr'
alert(jQuery(e).html()); //undefined
alert(jQuery(e).prop("tagName")); //undefined
alert(e.target); //undefined
alert(jQuery(e).target); //undefined
}
<script/>
Run Code Online (Sandbox Code Playgroud)
问题:
如何获得目标?!谢谢
jQuery版本应该如下所示:
jQuery.ajax({
url:"/Home/InsertModel",
data:"some post data",
type:"POST",
success:function(data){
doWork(this,data); // so …Run Code Online (Sandbox Code Playgroud) 假设我们有两个具有相同构造函数的类Injectable依赖:
public class FirstClass
{
public FirstClass(ISomeDependency someDependency)
{ }
}
public class SecondClass
{
public SecondClass(ISomeDependency someDependency)
{ }
}
Run Code Online (Sandbox Code Playgroud)
现在我们注册ISomeDependency:
builder.Register(x =>
{
string key = GetKeyFromCurrentHttpRequest();
// if "Caller" is "FirstClass" return new Dependency(key);
// else return new Dependency("defaultKey");
}).As<ISomeDependency>();
Run Code Online (Sandbox Code Playgroud)
注意:这是一个简化的用例.真实场景要复杂得多.
dependency-injection ioc-container inversion-of-control autofac
我已经看到许多使用IoC容器的代码示例,注册如下:
// Autofac
builder.Register(c => new HttpContextWrapper(HttpContext.Current))
.As<HttpContextBase>()
.InstancePerRequest();
// Again Autofac
builder.RegisterModule(new AutofacWebTypesModule());
Run Code Online (Sandbox Code Playgroud)
(请参阅此处的AutofacWebTypesModule的src )
// Castle Windsor
container.Register(Component.For<HttpContextBase()
.LifeStyle.PerWebRequest
.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Run Code Online (Sandbox Code Playgroud)
与使用构造函数注入的控制器一起:
public class HomeController : Controller
{
private readonly HttpContextBase _httpContext;
public HomeController(HttpContextBase httpContext)
{
_httpContext = httpContext;
}
//....
}
Run Code Online (Sandbox Code Playgroud)
你能解释一下包装HttpContextBase,HttpRequestBase等的原因吗?
注入的HttpContextBase与HttpContext(Controller的属性)与System.Web.HttpContext.Current之间的区别是什么?
在代码中使用哪个HttpContext,Injected One或者它也可以通过HttpContext和System.Web.HttpContext.Current调用它?如果两种方式都有问题吗?
asp.net-mvc dependency-injection castle-windsor inversion-of-control autofac
autofac ×2
reactjs ×2
typescript ×2
asp.net-mvc ×1
azure-devops ×1
c# ×1
javascript ×1
jquery ×1
next.js ×1
react-redux ×1
react-router ×1
redux ×1