typescript - Property 'searchText' has no initializer and is not definitely assigned in the constructor?
问题描述
There is following variable in class:
public searchText: string;
When strict mode is enabled in TypeScript, I see the following error:
Property 'searchText' has no initializer and is not definitely assigned in the constructor
In fact when I put ''
like:
public searchText: string = "";
It solves my problem, but why if default definition string is alreadyempty string;
解决方案
if you dont assign value to a variable in javascript then variables have 'undefined' value in it.
so here if you do like this
public searchText: string;
then searchText=undefined
and not an empty string. You can do console log you will get undefined
推荐阅读
- keras - AttributeError:模块“tensorflow”没有属性“convert_to_tensor”
- python-3.x - 当提示猜测 0 到 9 之间的数字时,用户猜对数字时无法获得成功消息
- android - Firebase cpp与clang ++ android链接
- html-table - html表中列的自动宽度
- c# - 如何在 if 语句中使用 TMPro 文本?
- javascript - 如何从字符串中删除第一个和最后一个字符?
- javascript - 使用 javascript 事件处理程序更改 html 输入图像
- react-native - 如何让视频播放器在世博会上做出反应?
- python - 使用来自不同时间序列的索引和值创建一个时间序列,具有不同的索引
- android - 使用 Android 导航组件在底部导航中的多个片段之间共享视图模型