济南网站建设哪家便宜,网站设计软件有哪些,网站插件代码下载,深圳建站工作室官网文档地址#xff1a;绑定 / Each 块绑定 • Svelte 教程 | Svelte 中文网 1、样式
一般情况下父子组件内样式隔离、同级组件间样式隔离
2、页面布局
styleP{color: red;}
/styescript
// 类似data
let name ‘jiang’
let countVal 0
let s…官网文档地址绑定 / Each 块绑定 • Svelte 教程 | Svelte 中文网 1、样式
一般情况下父子组件内样式隔离、同级组件间样式隔离
2、页面布局
styleP{color: red;}
/styescript
// 类似data
let name ‘jiang’
let countVal 0
let src ‘https://www.baidu.com’// 类似methods
let count () {countVal
}
/script// 直接布局
// 变量使用
div{name}/div
div on:click{count}{countVal}/div
// 属性名和变量名一样可以简写
img {src} alt“img” /3、v-html vs html contenteditabletrue bind:innerHTML
let string ‘dd pdsdsd/p’
p{html string}/p或者
scriptlet html pWrite some text!/p;
/script
divcontenteditabletruebind:innerHTML{html}
/div4、 methods
script
let count 0;
function handleClick() {count 1;
}
/script
button on:click{handleClick}Clicked {count} {count 1 ? time : times}/button5、computed vs $:
这里的computed是由赋值语句触发的所以变量需要用赋值语句修改值则computed变量能监听到变化
# 1
scriptlet count 0;// 定义computed变量$: doubled count * 2;function handleClick() {count 1;}
/script
button on:click{handleClick}Clicked {count} {count 1 ? time : times}
/button
p{count} doubled is {doubled}/p# 2
当count改变输出log
$: console.log(the count is ${count});# 3
你可以轻松地将一组语句组合成一个代码块
$: {console.log(the count is ${count});alert(I SAID THE COUNT IS ${count});
}# 4
computed里面监听某个data的数据时候满足一定条件执行相应的逻辑
scriptlet count 0;$: if (count 5) { 类似watchalert(count is dangerously high!);count 4;}function handleClick() {count 1;}
/script
button on:click{handleClick}Clicked {count} {count 1 ? time : times}
/button
6、 props 属性 vs export let answer
子组件
script// props属性export let answer;
/script
pThe answer is {answer}/p父组件
scriptimport Nested from ./Nested.svelte;
/script
Nested answer{42}/7、v-if vs 按条件渲染 {#if 为true变量}…{/if} {:else if 条件} {:else 条件} {/if}
scriptlet user { loggedIn: false };function toggle() {user.loggedIn !user.loggedIn;}
/script
{#if user.loggedIn}button on:click{toggle}Log out/button
{/if}{#if !user.loggedIn}button on:click{toggle}Log in/button
{/if}或者
{#if user.loggedIn}button on:click{toggle}Log out/button
{:else}button on:click{toggle}Log in/button
{/if}多条件渲染
{#if x 10}p{x} is greater than 10/p
{:else if 5 x}p{x} is less than 5/p
{:else}p{x} is between 5 and 10/p
{/if}
8、 v-for vs {#each cats as cat,index} {#each cats as {name, id},index} {/each }
{#each cats as cat, i}lia target_blank hrefhttps://www.youtube.com/watch?v{cat.id}{i 1}: {cat.name}/a/li
{/each}为each块添加key
{#each things as thing (thing.id)}Thing current{thing.color}/
{/each}9、事件修饰符 on:click|preventDefault{handleClick}
scriptfunction handleClick() {alert(no more alerts)}
/scriptbutton on:click|preventDefault{handleClick}Click me
/button
10、$emit vs createEventDispatcher 子组件给父组件传递消息
子组件
scriptimport { createEventDispatcher } from svelte;const dispatch createEventDispatcher();function sayHello() { // 给父组件抛出事件dispatch(message, {text: Hello!});}
/script
button on:click{sayHello}Click to say hello
/button注意父组件接收到的信息放在event.detail中
父组件
scriptimport Inner from ./Inner.svelte;function handleMessage(event) {alert(event.detail.text);}
/script
Inner on:message{handleMessage}/事件转发可以在中间组件用Inner on:message/默认转发所有message事件
11、表单元素
input bind:value
scriptlet name world;
/script
input bind:value{name}
h1Hello {name}!/h1p{a} {b} {a b}/p
显示12 3复选框
input typecheckbox bind:checked{yes}
绑定更多值 bind:group{flavours}
scriptlet scoops 1;let flavours [Mint choc chip];function join(flavours) {if (flavours.length 1) return flavours[0];return ${flavours.slice(0, -1).join(, )} and ${flavours[flavours.length - 1]};}
/scripth2Size/h2labelinput typeradio group{scoops} value{1}One scoop
/labellabelinput typeradio group{scoops} value{2}Two scoops
/labellabelinput typeradio group{scoops} value{3}Three scoops
/labelh2Flavours/h2labelinput typecheckbox group{flavours} valueCookies and creamCookies and cream
/labellabelinput typecheckbox group{flavours} valueMint choc chipMint choc chip
/labellabelinput typecheckbox group{flavours} valueRaspberry rippleRaspberry ripple
/label{#if flavours.length 0}pPlease select at least one flavour/p
{:else if flavours.length scoops}pCant order more flavours than scoops!/p
{:else}pYou ordered {scoops} {scoops 1 ? scoop : scoops}of {join(flavours)}/p
{/if}12、ref. vs bind:this
获取元素或者组件对象
scriptimport { onMount } from svelte;let canvas;onMount(() {// 挂载之后才会获取到元素const ctx canvas.getContext(2d);});
/scriptstylecanvas {width: 100%;height: 100%;background-color: #666;-webkit-mask: url(svelte-logo-mask.svg) 50% 50% no-repeat;mask: url(svelte-logo-mask.svg) 50% 50% no-repeat;}
/stylecanvasbind:this{canvas}width{32}height{32}
/canvas13、过渡动画效果 transition
scriptimport { fade } from svelte/transition;let visible true;
/scriptlabelinput typecheckbox bind:checked{visible}visible
/label{#if visible}p transition:fadeFades in and out/p
{/if}
上下渐入渐出 fly
scriptimport { fly } from svelte/transition;let visible true;
/scriptlabelinput typecheckbox bind:checked{visible}visible
/label{#if visible}p transition:fly{{ y: 200, duration: 2000 }}Flies in and out/p
{/if}
14、过渡事件
ptransition:fly{{ y: 200, duration: 2000 }}on:introstart{() status intro started}on:outrostart{() status outro started}on:introend{() status intro ended}on:outroend{() status outro ended}
Flies in and out
/p
15、组件slots
使用组件不传内容用默认显示
如果有自定义则显示自定义内容
默认插槽
slotpsdsd/p/slot
子组件Box.svelte
div classboxslotpdsdsds/p/slot
/div父组件
scriptimport Box from ./Box.svelte;
/script// 显示这里定义的内容
Boxh2Hello!/h2pThis is a box. It can contain anything./p
/Box// 显示slot默认内容
Box/Box
具名插槽
slot nameaddresspsdsd/p/slot
// 子组件
article classcontact-cardh2slot namenamespan classmissingUnknown name/span/slot/h2div classaddressslot nameaddressspan classmissingUnknown address/span/slot/divdiv classemailslot nameemailspan classmissingUnknown email/span/slot/div
/article// 父组件
ContactCardspan slotnameP. Sherman/spanspan slotaddress42 Wallaby WaybrSydney/span
/ContactCard
16、动态组件
svelte:component this{selected.component}/
scriptimport RedThing from ./RedThing.svelte;import GreenThing from ./GreenThing.svelte;import BlueThing from ./BlueThing.svelte;const options [{ color: red, component: RedThing },{ color: green, component: GreenThing },{ color: blue, component: BlueThing },];let selected options[0];
/scriptselect bind:value{selected}{#each options as option}option value{option}{option.color}/option{/each}
/selectsvelte:component this{selected.component}/