我使用AMD模块,我想在一个文件后面隐藏一个复杂的接口,该文件加载其他几个文件并选择要公开的内容和方式.它有效,我使用这个解决方案,但感觉有点难看,主要是接口.
import Types = require('./message-types');
import MessageBaseImport = require('./message-base');
export interface IMessage extends Types.IMessage {} // This is an interface
export var MessageBase = MessageBaseImport; // This is a class
Run Code Online (Sandbox Code Playgroud)
用法:
import Message = require('message');
import { * } as Message from 'message'; // Or with ES6 style
var mb = new Message.MessageBase(); // Using the class
var msg: Message.IMessage = null; // Using the interface
Run Code Online (Sandbox Code Playgroud)
还有更好的解决方案吗?我不想将所有内容都放在一个文件中,但我想要import一个文件.
我理解ES6 标记模板的语法.我看不到的是实用性.什么时候比传递一个对象参数更好,比如jQuery的AJAX中的设置?$.ajax('url', { /*this guy here*/ })
现在我只看到棘手的语法,但我不明白为什么我需要/使用它.我还发现TypeScript团队选择在其他重要功能之前实现它(在1.5中).标记字符串模板背后的概念是什么?
我有一个主模块和一些子模块.我想在它们之间指定一些不平凡的路由.
我更喜欢在子模块中定义子模块的路径.例如:
@NgModule({
imports: [
/*...*/
RouterModule.forChild([
{ path: 'country', redirectTo: 'country/list' },
{ path: 'country/list', component: CountryListComponent },
{ path: 'country/create', component: CountryCreateComponent },
/*...*/
])
],
declarations: [/*...*/],
exports: [
RouterModule,
],
})
export class CountryModule {}
Run Code Online (Sandbox Code Playgroud)
我想用自己的内部路由导入这个模块,但我想让它的整个路由前缀.
const appRoutes = [
{ path: '', component: HomeComponent },
/*... (basic routes)*/
];
@NgModule({
imports: [
/*...*/
RouterModule.forRoot(appRoutes),
CountryModule, // <- how to make its routing prefixed??
],
declarations: [
/*...*/
AppComponent,
],
bootstrap: [ AppComponent ]
})
export …Run Code Online (Sandbox Code Playgroud) 是否可以在同一属性上使用公共 init 访问器和私有 setter?
目前我收到错误CS1007 “已定义属性访问器”。
public record Stuff
{
public int MyProperty { get; init; private set; } // Error
public void SetMyProperty(int value) => MyProperty = value;
}
var stuff = new Stuff
{
MyProperty = 3, // Using the init accessor
};
stuff.SetMyProperty(4); // Using the private setter (indirectly)
Run Code Online (Sandbox Code Playgroud)
我最好的猜测是使用私有成员变量,该变量的属性get和init访问器(不是自动实现的)和 setter 成员函数。可以更轻松地完成吗?
我正在构建一个模块化的网络应用程序,其中要处理各种用户创建的模型。Shadow DOM 是一个明显的选择,但我不明白 JS 如何在 Shadow DOM 中执行。
我有一个 HTML 内容要使用以下虚拟脚本加载。
<h1>Nice header</h1>
<script type="text/javascript">
console.log('hello')
alert('hi');
</script>
Run Code Online (Sandbox Code Playgroud)
我使用之前的脚本加载页面,如下所示:
<div id="shadow-target"></div>
<div id="non-shadow-target"></div>
<script>
// Called on btn click
loadShadow = function(module) {
var root = document.querySelector('#shadow-target').createShadowRoot();
var lighttarget = document.querySelector('#non-shadow-target');
$.get('whatever.html', function (data) {
alert(data);
$(root).append(data); // Nothing executed
$(lighttarget).append(data); // Works fine
// The header appears in both cases
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
正如评论所说,当内容插入到影子 DOM 根时,JS 不会被执行。标头在两种情况下都会出现,但脚本仅在 light DOM 中执行。这是为什么?自定义JS如何在shadow DOM中执行?(没有跨域的东西。)
是否不可能利用“ON CONFLICT DO NOTHING”来避免插入和违反非空约束?
例如...
INSERT INTO public.users (user, user_yob, sex)
SELECT mom, mom_yob, 'F'
FROM staging.users
ON CONFLICT DO NOTHING;
Run Code Online (Sandbox Code Playgroud)
产生此错误并停止...
INSERT INTO public.users (user, user_yob, sex) SELECT mom, mom_yob, 'F' FROM staging.users ON CONFLICT DO NOTHING
> ERROR: null value in column "user" violates not-null constraint
DETAIL: Failing row contains (0b159b81-6842-4ae7-961c-2e9cff8488b1, null, null, null, null, null, null, null, null, null, F).
Run Code Online (Sandbox Code Playgroud)
理想情况下,这个特定的插入将被忽略而不是引发错误。
如果Angular2遇到内部异常,它将不会记录到控制台.如何检测以下异常?
EXCEPTION: Error during instantiation of MainFormComponent!.
ORIGINAL EXCEPTION: TypeError: Cannot set property 'crashMeMaybe' of undefined
ORIGINAL STACKTRACE:
... stacktrace ...
ERROR CONTEXT:
... context object ...
Run Code Online (Sandbox Code Playgroud)
有没有可用的订阅?这样的文件在哪里?
我有一个带SocketIO的节点服务器和几个连接.如何根据其ID断开现有套接字?每个房间都有房间,管理员和其他连接.我的目标是使管理套接字根据其ID使任何其他套接字断开连接.
解决方法是将消息发送到要踢回服务器的客户端:io.to(socketId).emit('commit-suicide!');然后它发送回"I-am-so-suicid"消息并且服务器调用socket.disconnect().现在这显然不是最佳解决方案......
angular ×2
javascript ×2
bitbucket ×1
c# ×1
c#-9.0 ×1
ecmascript-6 ×1
exception ×1
node.js ×1
postgresql ×1
shadow-dom ×1
socket.io ×1
typescript ×1