SvelteKitで親コンポーネントから子コンポーネントの関数を実行する

2023/07/18

子コンポーネントで、関数を export しておきます。

Child.svelte
<script lang="ts">export const hoge = () => {
    console.log("hoge");
};
</script>

<div>Child</div>

親コンポーネントから子コンポーネント利用する際に、下記のように子コンポーネントの関数を bind します。

+page.svelte
<script lang="ts">import Child from "components/Child.svelte";
let hoge = () => {
};
</script>

<Child bind:hoge />
<button on:click={hoge}>push</button>

このコードは試してないですが、大体こんな感じでできます️ 😊

Rust🦀, Network⚡, PostgreSQL🐘, Unity🎮

Tags

rust  (9)
rocket  (7)
svelte  (5)
c++  (4)
ethereum  (3)
solidity  (3)
vscode  (3)
sqlx  (3)
glfw  (2)
opengl  (2)
nestjs  (2)
graphql  (2)
render  (2)
wsl2  (2)
truffle  (1)
goerli  (1)
geth  (1)
hardhat  (1)
nft  (1)
gui  (1)
tetris  (1)
next.js  (1)
jwt  (1)
nextauth  (1)
node.js  (1)
prisma  (1)
passport  (1)
urql  (1)
codegen  (1)
mdsvex  (1)
markdown  (1)
tmux  (1)
nvim  (1)
axum  (1)
atcoder  (1)
vim  (1)
pacman  (1)
tracing  (1)
Cursor  (1)
VSCode  (1)
PHP  (1)
Laravel  (1)