我有以下声明:
我object从以下某个地方收到了一个例子:
paginationConfig = {
'currentPage': 1,
'pageSizes': [5, 10, 15],
'perPage': 20
};
Run Code Online (Sandbox Code Playgroud)
我的课:
export class MyComponent implements OnInit {
currentPage: number;
pageSizes: number[];
perPage: number;
constructor() { }
ngOnInit(): void {
{ this.currentPage, this.perPage, this.pageSizes } = paginationConfig;
// It prints undefined for all those variables and I receive the error:
// TS1128: Declaration or statement expected.
// It works for local variables (as below), why not when I use **this**?
const { currentPage, perPage, pageSizes …Run Code Online (Sandbox Code Playgroud) 我正在使用文本掩码库,它运行得很好.
考虑以下掩码配置:
priceMask = Object.freeze({
mask: createNumberMask({
allowDecimal: true,
decimalSymbol: ',',
integerLimit: 7,
prefix: '',
thousandsSeparatorSymbol: '.'
})
});
Run Code Online (Sandbox Code Playgroud)
在我的HTML中,我有以下内容:
<form [formGroup]="formGroup">
<input type="text"
formControlName="maskedInput"
[textMask]="priceMask">
</form>
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到,在我的掩码配置中,我将字段限制为具有如下值:
9.999.999,99
但是,虽然我想向用户显示这种特定的格式,但我希望在我的内容中获得不同的值control,例如:
9999999,99
这可能吗?
我希望这个问题足够清楚.谢谢.
这是我创建的一个plnkr来说明情况.
所以我用来Jest#test.each运行一些单元测试。
这是实际的代码:
const invalidTestCases = [
[null, TypeError],
[undefined, TypeError],
[false, TypeError],
[true, TypeError],
];
describe('normalizeNames', () => {
describe('invalid', () => {
test.each(invalidTestCases)('some description for (%p, %p)', (actual, expected) => {
expect(() => normalizeNames(actual as any)).toThrowError(expected);
});
});
describe('valid', () => {
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
问题是由于打字稿错误我无法运行它:
Argument of type '(actual: boolean | TypeErrorConstructor | null | undefined, expected: boolean | TypeErrorConstructor | null | undefined) => void' is not assignable to parameter of type '(...args: (TypeErrorConstructor …Run Code Online (Sandbox Code Playgroud) 我正在使用 sweetAlert2,并尝试使用 bootstrap 4 来设置按钮样式,设置属性:
buttonsStyling: false,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg'
Run Code Online (Sandbox Code Playgroud)
它有效,但是showLoaderOnConfirm当我设置上面的这些属性时,该选项以非常难看的样式显示。
您可以检查以下示例:
重现步骤:
buttonsStyling: false,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg'
Run Code Online (Sandbox Code Playgroud)
$(function() {
$('#button').click(() => {
swal({
title: 'Submit email to run ajax request',
input: 'email',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
buttonsStyling: false,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg',
preConfirm: function(email) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (email === 'taken@example.com') …Run Code Online (Sandbox Code Playgroud)我有很多带有静态变量的类,如下所示:
用户.ts:
export class User {
static modelKey = 'user';
// other variables
}
Run Code Online (Sandbox Code Playgroud)
汽车.ts:
export class Car {
static modelKey = 'car';
// other variables
}
Run Code Online (Sandbox Code Playgroud)
在某个地方,我只想像这样调用DataSource(见下文):
const dataSource = new DataSource<Car>();
Run Code Online (Sandbox Code Playgroud)
数据源.ts:
export class DataSource<T> {
constructor() {
console.log(T.modelKey); // won't compile
}
}
Run Code Online (Sandbox Code Playgroud)
当然,它不会编译,因为我不能简单地使用T.<variable>. 所以,我的问题是:我怎样才能做到这一点?
我有一些模块的应用程序.
这是我的根模块:
// imports...
@NgModule({
bootstrap: [ AppModule ],
declarations: [
AppComponent,
...APP_PIPES // Array with my pipes
],
imports: [ // import Angular's modules
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
RouterModule.forRoot(ROUTES),
...APP_MODULES, // Array with my modules
...THIRD_PARTY_MODULES // Array with libs modules
],
providers: [ // expose our Services and Providers into Angular's dependency injection
ENV_PROVIDERS,
APP_PROVIDERS
]
})
export class AppModule {
...
}
Run Code Online (Sandbox Code Playgroud)
而这里的ONE的我的共同模块:
import { CommonModule } from '@angular/common';
import { NgModule } …Run Code Online (Sandbox Code Playgroud) pt-BR.yml:
\n\npt-BR:\n activerecord:\n models:\n user: Usu\xc3\xa1rio\n project: Projeto\n attributes:\n user:\n name: O nome\n description: A descri\xc3\xa7\xc3\xa3o\n projects: Os projetos\n project:\n name: O nome\n errors:\n format: "%{attribute} %{message}"\n messages:\n accepted: deve ser aceito\n blank: n\xc3\xa3o pode ficar em branco\n ........\nRun Code Online (Sandbox Code Playgroud)\n\n楷模:
\n\nclass User < ApplicationRecord\n has_many :projects, dependent: :destroy, inverse_of: :user\n\n accepts_nested_attributes_for :projects, allow_destroy: true\nend\n\nclass Project < ApplicationRecord\n belongs_to :user, inverse_of: :projects\n\n validates :name, presence: true, length: { mininum: 3, maximum: 255 }\nend\nRun Code Online (Sandbox Code Playgroud)\n\n控制器:
\n\ndef …Run Code Online (Sandbox Code Playgroud) 我有一个字符串,我只想检查它是否是"模型"......所以在搜索后我找到了一种方法:
'any_name'.classify.constantize
Run Code Online (Sandbox Code Playgroud)
但是......当它不是有效的模型名称时,会抛出以下错误:
NameError(错误的常量名称AnyName):
所以我尝试做以下事情:
if Object.const_defined?('AnyName')
#...
end
# I also tried this:
Object.const_get('AnyName')
Run Code Online (Sandbox Code Playgroud)
但同样,上述两个选项都返回相同的错误:
NameError(错误的常量名称AnyName):
该const_defined不该只返回真/假,而不是抛出一个错误?
目前,我发现了这个丑陋的解决方法:
'any_name'.classify.constantize rescue nil
Run Code Online (Sandbox Code Playgroud)
但AFAIK并不是一个好习惯(也是rubocop声称这个).
所以,我的问题是......有一种安全的方法吗?
我正在使用宝石active_model_serializers.
串行器:
class ProjectGroupSerializer < ActiveModel::Serializer
attributes :id, :name, :description
has_many :projects
end
class ProjectSerializer < ActiveModel::Serializer
attributes :id, :name, :description
belongs_to :project_group
end
Run Code Online (Sandbox Code Playgroud)
控制器:
def index
@project_groups = ProjectGroup.all.includes(:projects)
render json: @project_groups, include: 'projects'
end
Run Code Online (Sandbox Code Playgroud)
我收到以下回复:
{
"data": [
{
"id": "35",
"type": "project_groups",
"attributes": {
"name": "sdsdf",
"description": null
},
"relationships": {
"projects": {
"data": [
{
"id": "1",
"type": "projects"
},
{
"id": "2",
"type": "projects"
}
]
}
}
}
],
"included": …Run Code Online (Sandbox Code Playgroud) angular ×4
typescript ×4
javascript ×2
activemodel ×1
css ×1
generics ×1
jestjs ×1
ruby ×1
sweetalert ×1
sweetalert2 ×1
ts-jest ×1