我再次写我的问题,因为早些时候它没有意义,我不是很清楚.
我从API接收的数据看起来像这样:
{"photos":[{"id":1,"title":"photo_1_title"}]}
Run Code Online (Sandbox Code Playgroud)
所以,在我的代码中,我有一个photos变量和一个名为的方法getPhotos()
我使用无限滚动,所以当我到达页面底部时,我getPhotos()再次打电话.
photos: any;
getPhotos() {
this.photoService.getPhotos()
.subscribe(
photos => this.photos = photos
// here, instead of doing this, I want to add the array of photos I get back to this.photos using Object.assign however it is giving me the said error
)
}
Run Code Online (Sandbox Code Playgroud)
因此,如果下次我打电话给它,我会回来{"photos":[{"id":2,"title":"photo_2_title"}]},然后我会尝试设置this.photos为
{"photos":[{"id":1,"title":"photo_1_title"}, {"id":2,"title":"photo_2_title"}]}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解释为什么
jsfiddle.net/ca46hLw9不起作用?我认为assign应该合并一个对象的内容吗?
有没有办法将对象类型转换为类类型,以便遵守默认属性值?
例如:
class Person {
name: string = "bob";
age: number;
sex: string;
}
var data = {
"age": 23,
"sex": "male"
}
var p = <Person>data;
console.log(p.name); // -> undefined
Run Code Online (Sandbox Code Playgroud)
p.name鲍勃最简单的方法是什么?
*编辑
这似乎有效:
var record : Person = Object.assign(new Person(), p);
Run Code Online (Sandbox Code Playgroud)
这是好还是坏的做法?
假设我有一个非常简单的类,带有计算属性:
class Person {
firstName: string;
lastName: string;
get fuillName(): string {
return this.firstName + ' ' + this.lastName;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个类型的对象Person:
let p: Person = {
firstName: 'John',
lastName: 'Smith'
};
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
输入'{firstName:string; lastName:string; }'不能赋值为'Person'.类型'{firstName:string;中缺少属性'fuillName'; lastName:string; }".
咦?fullName是一个只读属性.所以我按照这个问题并实现了一个部分初始化器:
constructor(init?: Partial<Person>) {
Object.assign(this, init);
}
Run Code Online (Sandbox Code Playgroud)
同样的错误.我知道我可以这样做:
let p = new Person();
p.firstName = 'John';
p.lastName = 'Smith';
console.debug(p.fullName);
Run Code Online (Sandbox Code Playgroud)
但是有没有使用JSON语法初始化类的简写?
因此,在 c# 中,您可以使用对象初始值设定项语法使用值实例化一个类。在 TypeScript 中,似乎没有相同类型的对象初始值设定项语法。我发现您可以使用以下两种方法初始化值:
构造函数初始化:
class MyClass {
constructor(num: number, str: string) {
this.numProperty = num;
this.strProperty = str;
}
numProperty: number;
strProperty: string;
}
let myClassInstance = new MyClass(100, 'hello');
Run Code Online (Sandbox Code Playgroud)
对象类型转换:
class MyClass {
numProperty: number;
strProperty: string;
}
let myClassInstance = <MyClass>{
numProperty: 100,
strProperty: 'hello'
};
Run Code Online (Sandbox Code Playgroud)
虽然我喜欢在 TypeScript 中使用 Object Type Casting 语法,但它仅适用于没有您需要使用的方法的简单 DTO 类。这是因为转换实际上并没有创建您要转换到的类类型的对象。
还有其他方法可以在 TypeScript 中进行对象初始化吗?
在下面的方法中,我从 Firebase 集合中检索文档。
我已成功记录调用时需要返回的值getUserByUserId(),但我需要将它们作为User对象返回:
getUserByUserId(userId: string) {
return of(firebase.firestore().collection("users").where("userId", "==", userId)
.get().then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log("User ID", "=>", doc.data().userId);
console.log("Username", "=>", doc.data().userName);
console.log("Mechanic", "=>", doc.data().isMechanic);
console.log("Location", "=>", doc.data().location);
})
}).catch(err => {
console.log(err);
}));
}
Run Code Online (Sandbox Code Playgroud)
以下是User数据需要遵循的结构:
import { PlaceLocation } from './location.model';
export class User {
constructor(
public id: string,
public name: string,
public isMechanic: boolean,
public email: string,
public location: PlaceLocation
) { }
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何User使用此数据创建一个对象并将其作为响应返回吗getUserByUserId()?