Sin*_*rus 20 typescript vue.js
$refs!: {
helloComponent: Hello
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/vuejs/vue-class-component/blob/master/example/App.vue
Har*_*til 24
当TypeScript认为某个属性变量是null或时,会出现这种情况undefined.但是,如果您确定此变量不能为null,则可以使用此运算符.
考虑这个例子:
let a = document.getElementById('hello');
if (a) {
a.style.width = '100px';
}
Run Code Online (Sandbox Code Playgroud)
TypeScript假定该变量a可能为null,因为无法保证此元素存在.因此,在您访问该变量之前,您已经if提防了.但是如果你知道你的应用程序总是有一个带id的HTML元素#hello,那么你可以将上面的代码重写为:
const a = document.getElementById('hello');
a!.style.width = '100px';
Run Code Online (Sandbox Code Playgroud)
上面的代码更易读,更简洁.欲了解更多信息,请访问:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html
这是一个“明确分配断言”:varname !: sometype通知打字稿不要担心是否varname可能未分配(它告诉打字稿varname肯定会被分配,即使打字稿无法推断出分配在哪里)。通常,打字稿会检查该变量是否未分配,并给出错误。
有关更多信息,请参见:https : //www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#definite-assignment-assertions
| 归档时间: |
|
| 查看次数: |
9068 次 |
| 最近记录: |