Svelte基础教程
没有特殊说明,下面的每个例子均是一个单独的
.svelte文件。
# 基础篇
# 最基础的例子
输出 Hello world。
<h1>Hello world!</h1>htmlcopy success
# 动态数据
不想跟 world 打招呼了,想跟别人打招呼。
<script> const name = 'realign'; </script> <h1>Hello {name}!</h1>htmlcopy success
# 动态属性
给打招呼的人加个头像。
<script> const name = 'realign'; const avatar = 'https://svelte.dev/favicon.png'; </script> <h1>Hello {name}!</h1> <img src={avatar} alt="" />htmlcopy success" data-mdic-attach-content="作者:安亮军 @ReAlign 来源:杂记 | https://blog.realign.cn 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。" data-mdic-notify-id="j-notify-1766983174458-34605" data-mdic-notify-delay="2000" data-mdic-copy-fail-text="copy fail" onclick="!function(t){const e={copy:(t='',e='')=>new Promise((c,o)=>{const n=document.createElement('textarea'),d=e?`\n\n${e}`:e;n.value=`${t}${d}`,document.body.appendChild(n),n.select();try{const t=document.execCommand('copy');document.body.removeChild(n),t?c():o()}catch(t){document.body.removeChild(n),o()}}),btnClick(t){const c=t&&t.dataset?t.dataset:{},o=c.mdicNotifyId,n=document.getElementById(o),d=c.mdicNotifyDelay,i=c.mdicCopyFailText;e.copy(c.mdicContent,c.mdicAttachContent).then(()=>{n.style.display='block',setTimeout(()=>{n.style.display='none'},d)}).catch(()=>{alert(i)})}};e.btnClick(t)}(this);">copy
# 样式渲染
给头像设置大小跟边框。
<script> const name = 'realign'; const avatar = 'https://svelte.dev/favicon.png'; </script> <h1>Hello {name}!</h1> <img src={avatar} alt="" /> <style> img { width: 30px; border: 1px solid #ff3e00; } </style>htmlcopy success" data-mdic-attach-content="作者:安亮军 @ReAlign 来源:杂记 | https://blog.realign.cn 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。" data-mdic-notify-id="j-notify-1766983174458-3211" data-mdic-notify-delay="2000" data-mdic-copy-fail-text="copy fail" onclick="!function(t){const e={copy:(t='',e='')=>new Promise((c,o)=>{const n=document.createElement('textarea'),d=e?`\n\n${e}`:e;n.value=`${t}${d}`,document.body.appendChild(n),n.select();try{const t=document.execCommand('copy');document.body.removeChild(n),t?c():o()}catch(t){document.body.removeChild(n),o()}}),btnClick(t){const c=t&&t.dataset?t.dataset:{},o=c.mdicNotifyId,n=document.getElementById(o),d=c.mdicNotifyDelay,i=c.mdicCopyFailText;e.copy(c.mdicContent,c.mdicAttachContent).then(()=>{n.style.display='block',setTimeout(()=>{n.style.display='none'},d)}).catch(()=>{alert(i)})}};e.btnClick(t)}(this);">copy
# 头像单独放
想把头像单独抽离,给不欢迎的人的名单用。
<!-- avatar.svelte --> <script> const avatar = 'https://svelte.dev/favicon.png'; </script> <img src={avatar} alt="" /> <style> img { width: 30px; border: 1px solid #ff3e00; } </style>htmlcopy success" data-mdic-attach-content="作者:安亮军 @ReAlign 来源:杂记 | https://blog.realign.cn 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。" data-mdic-notify-id="j-notify-1766983174461-42592" data-mdic-notify-delay="2000" data-mdic-copy-fail-text="copy fail" onclick="!function(t){const e={copy:(t='',e='')=>new Promise((c,o)=>{const n=document.createElement('textarea'),d=e?`\n\n${e}`:e;n.value=`${t}${d}`,document.body.appendChild(n),n.select();try{const t=document.execCommand('copy');document.body.removeChild(n),t?c():o()}catch(t){document.body.removeChild(n),o()}}),btnClick(t){const c=t&&t.dataset?t.dataset:{},o=c.mdicNotifyId,n=document.getElementById(o),d=c.mdicNotifyDelay,i=c.mdicCopyFailText;e.copy(c.mdicContent,c.mdicAttachContent).then(()=>{n.style.display='block',setTimeout(()=>{n.style.display='none'},d)}).catch(()=>{alert(i)})}};e.btnClick(t)}(this);">copy
<!-- main.svelte --> <script> import Avatar from './avatar.svelte'; const name = 'realign'; </script> <h1>Hello {name}!</h1> <Avatar />htmlcopy success
未完,待完善...