如何通过 getElementById 获取类型?
How do I get the Type via getElementById?
这是我在 Svelte 项目中的脚本标签。
<script lang="ts">
import { onMount } from 'svelte';
onMount(async () => {
await import('@material/mwc-drawer');
});
function onNavClick() {
const drawer = this.getElementById('mat-drawer');
}
</script>
我想在这里做的是在我的元素上打字,为了学习 id = mat-drawer
。
最后要么我要这样做:
const drawer = this.getElementById('mat-drawer') as Drawer;
或
const drawer: Drawer = this.getElementById('mat-drawer')!;
但我不知道如何在此上下文中导入 Drawer
?
编辑:
我希望我只在这里寻找类型信息。然而这个:
<script lang="ts">
import { onMount } from 'svelte';
import { Drawer } from '@material/drawer';
onMount(async () => {
await import('@material/mwc-drawer');
});
function onNavClick() {
const drawer = this.getElementById('mat-drawer') as Drawer;
}
</script>
失败:
This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'.
为什么,你对某件事感到压力很长一段时间,然后撞到 Stack Overflow,然后在最后一根稻草的过程中,你找到了解决方案?
解决方案是import type
。这辈子没见过,现在知道了,自然明白为什么会这样了。
<script lang="ts">
import { onMount } from 'svelte';
import type { MDCDrawer } from '@material/drawer';
onMount(async () => {
// use the custom component in markup
await import('@material/mwc-drawer');
});
function onNavClick() {
// get the actual type in typescript
const drawer = this.getElementById('mat-drawer') as MDCDrawer;
}
</script>
这是我在 Svelte 项目中的脚本标签。
<script lang="ts">
import { onMount } from 'svelte';
onMount(async () => {
await import('@material/mwc-drawer');
});
function onNavClick() {
const drawer = this.getElementById('mat-drawer');
}
</script>
我想在这里做的是在我的元素上打字,为了学习 id = mat-drawer
。
最后要么我要这样做:
const drawer = this.getElementById('mat-drawer') as Drawer;
或
const drawer: Drawer = this.getElementById('mat-drawer')!;
但我不知道如何在此上下文中导入 Drawer
?
编辑:
我希望我只在这里寻找类型信息。然而这个:
<script lang="ts">
import { onMount } from 'svelte';
import { Drawer } from '@material/drawer';
onMount(async () => {
await import('@material/mwc-drawer');
});
function onNavClick() {
const drawer = this.getElementById('mat-drawer') as Drawer;
}
</script>
失败:
This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'.
为什么,你对某件事感到压力很长一段时间,然后撞到 Stack Overflow,然后在最后一根稻草的过程中,你找到了解决方案?
解决方案是import type
。这辈子没见过,现在知道了,自然明白为什么会这样了。
<script lang="ts">
import { onMount } from 'svelte';
import type { MDCDrawer } from '@material/drawer';
onMount(async () => {
// use the custom component in markup
await import('@material/mwc-drawer');
});
function onNavClick() {
// get the actual type in typescript
const drawer = this.getElementById('mat-drawer') as MDCDrawer;
}
</script>