小编eri*_*ola的帖子

打字稿类中的方法给出错误"不是函数"

我在Angular2 Web应用程序上工作.我在typescript中创建了一个简单的类:

export class User {
    firstName: string;
    lastName: string;

    nominative() : string {
        return this.lastName + " " + this.firstName;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我调用nominative类型UserI的对象时收到此错误:Error in :0:0 caused by: user.nominative is not a function.

我在AppComponent班上调用这个函数:

export class AppComponent implements OnInit {
    name: string = "";

    ngOnInit() : void {
        let user = JSON.parse(sessionStorage.getItem("User")) as User;
        if (user) {
            this.name = user.nominative();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过以这种方式使用lambda表达式:

nominative = () : string => { ... …
Run Code Online (Sandbox Code Playgroud)

typescript angular

30
推荐指数
1
解决办法
1万
查看次数

EntityFramework Core Database.EnsureCreated不创建数据库

我用dotnetcore和实体框架核心创建了一个项目.我使用MySql数据库,我添加了对SapientGuardian.EntityFrameworkCore.MySql版本的依赖7.1.19.

我创建了一个SeedData初始化我的数据库的类:

public class SeedData
{
    public static void Initialize(IServiceProvider serviceProvider)
    {
        using (MyDbContext context = new MyDbContext(serviceProvider.GetRequiredService<DbContextOptions<MyDbContext>>())) {
            context.Database.EnsureCreated();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对种子类的调用:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // Other code

    SeedData.Initialize(app.ApplicationServices);
}
Run Code Online (Sandbox Code Playgroud)

和服务配置:

public void ConfigureServices(IServiceCollection services)
{
    // Other code

    // Add database
    services.AddDbContext<MyDbContext>(options => {
            options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"));
    });

    services.AddScoped<Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptions, Microsoft.EntityFrameworkCore.DbContextOptions<MyDbContext>>();
}
Run Code Online (Sandbox Code Playgroud)

当我启动项目时,我在EnsureCreate通话时遇到异常.异常消息说Authentication failed但如果我手动创建数据库(没有表),我的连接字符串工作正常,EnsureCreated只需为我的实体创建表.

怎么了?这是一个问题SapientGuardian.EntityFrameworkCore.MySql

c# mysql entity-framework-core .net-core

8
推荐指数
1
解决办法
1715
查看次数

在 Angular 11 库中添加扩展方法

不久前,我创建了两个简单的文件来扩展某些类型的功能。我global.d.ts用这种方式创建了一个:

export { }

declare global {
    interface Date {
        isLeapYear() : boolean;
    }

    interface Array<T> {
        contains(item: T, compareFunc?: (value: T) => boolean): boolean;
        sum(callbackfn: (value: T) => number) : number;
    }

    interface Element {
        closest(selector: string) : Element;
    }

    interface String {
        contains(text: string) : boolean;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我extensions.ts在原型上创建了文件和实现。我将其导入到我的polyfills.ts

import "zone.js/dist/zone";
import "./extensions";
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我可以在代码中的任何地方使用这些方法。现在我需要在 Angular 11 库中做同样的事情。我创建了global.d.tsextensions.ts,但是现在呢?没有polyfills.ts文件,如果我尝试进行简单的导入,而我需要其中一种方法 ( import "../../extensions";),则不起作用。在控制台中我收到此错误:

projects/test_library/src/components/test.component/test.component.ts:428:48 - error TS2339: Property …
Run Code Online (Sandbox Code Playgroud)

typescript angular angular-library angular11

6
推荐指数
0
解决办法
1026
查看次数