onMount 生命周期内的语句和 onMount 生命周期外的语句有什么区别?
What is the difference between statements inside onMount and ouside onMount lifecycle?
在我看来(我知道我会错的)我在 onMount
生命周期函数中所做的事情在 svelte 中也可以在它之外完成。有区别吗?或者..我错过了很多要点。
<script>
import {onMount} from 'svelte'
function doSomething() {
// ......
// ..........
}
onMount(() => {
function doSomething() {
// ......
// ..........
}
})
</script>
如 API documentation 中所述,onMount
将 运行 组件添加到 DOM 时,然后才添加。
onMount
回调外的语句 运行 将在挂载组件时执行,而且当组件在 SSR 中 运行 时也会执行。
通常你在 onMount
回调语句中放入需要 DOM 可用的回调语句,或者由于各种原因在使用 SSR 时无法在服务器端执行。
在我看来(我知道我会错的)我在 onMount
生命周期函数中所做的事情在 svelte 中也可以在它之外完成。有区别吗?或者..我错过了很多要点。
<script>
import {onMount} from 'svelte'
function doSomething() {
// ......
// ..........
}
onMount(() => {
function doSomething() {
// ......
// ..........
}
})
</script>
如 API documentation 中所述,onMount
将 运行 组件添加到 DOM 时,然后才添加。
onMount
回调外的语句 运行 将在挂载组件时执行,而且当组件在 SSR 中 运行 时也会执行。
通常你在 onMount
回调语句中放入需要 DOM 可用的回调语句,或者由于各种原因在使用 SSR 时无法在服务器端执行。