簡體   English   中英

如何在打字稿中將obj轉換為字符串

[英]How to cast obj in to string in typescript

我使用 Angular 13 並從 JSON 中獲取標題

this.variable = this.service('id', this.parentcontrol)

我從 JSON 獲得了 ID

然后我像這樣{{variable.title}}向 HTML 頁面發送響應

要么我使用了手風琴,而且每一行都有這個對象

{
question:'should be a string',
answer:'should be a string',
opened:false
}

我的問題是如何發送這個對象的字符串

this.variable = this.service('id', this.parentcontrol)

質疑?

我在打字稿中確實喜歡這個

question : String(variable.title);

但是當我寫喜歡的時候我得到了null

question: 'hello world"

其作品。

你能幫我解決這個問題嗎?

您可能不需要進行類型轉換,而像這樣的事情就可以了。

const myObject = {
    title: 'foo',
};

const question: string = myObject.title;
const question2: typeof myObject['title'] = myObject.title;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM