如何安装svelte_构建我的第一个Svelte应用程序:想法和印象

如何安装svelte

There are always going to be new JavaScript libraries and frameworks that pop up. Sometimes it's easy to feel that we are in a new JS library fatigue mode. It is an amazing thing though since this means more innovation across all of the JavaScript land.

总是会有新JavaScript库和框架弹出。 有时,很容易感觉到我们处于新的JS库疲劳模式。 尽管这是一件令人惊奇的事情,因为这意味着在所有JavaScript领域中都有更多的创新

Svelte v3 (by Rich Harris) hit the scenes back in April and it took the dev world by storm. Everywhere you looked on social media, Svelte was being talked about.

Svelte v3(由Rich Harris撰写 )早在4月就登上了舞台,这使开发界风靡一时。 在社交媒体上到处可见的内容都在谈论Svelte 。

如何安装svelte_构建我的第一个Svelte应用程序:想法和印象_第1张图片

I stayed away from trying out Svelte. Usually I'll dive head first immediately; this time I was a bit busy and thought I'd wait a bit. On yesterday's Twitch stream, I gave it a try on my Twitch channel.

我远离尝试斯维尔特。 通常我会立即先潜水; 这次我有点忙,以为我会等一下。 在昨天的Twitch直播中 ,我在Twitch频道上进行了尝试。

我的主要印象 ( My Main Impressions )

The syntax is so clean!** **For writing your JavaScript and your styles, it just makes sense. It's as close to vanilla JavaScript as it gets.

语法非常干净 !** **对于编写JavaScript和样式,这很有意义。 它尽可能接近原始JavaScript。

The most complex part **(although not that complex) is the templating**. Vue's directives are easy to grasp. JSX is closer to JavaScript. Svelte's seems like a bit of a learning curve.

最复杂的部分**(尽管不那么复杂是模板**。 Vue的指令很容易掌握。 JSX更接近JavaScript。 Svelte的学习曲线有点像。

I like how components and props are handled. I like how you just have to update a variable and it updates. No worrying about things like setState().

我喜欢组件和道具的处理方式。 我喜欢您只需要更新变量并更新它的方式。 不用担心诸如setState()类的事情。

I like how styles are automatically scoped. Haven't messed with trying to add Sass yet.

我喜欢样式如何自动设置范围。 还未尝试添加Sass的事情。

I still haven't gone in depth enough to judge overall. I'd really like to take Sapper, the Svelte framework, for a try.

我仍然没有足够的深度来判断整体。 我真的很想尝试Svelte框架Sapper 。

Overall very clean and succinct!

总体来说很干净简洁!

最终代码和演示 ( Final Code and Demo )

Here's the final CodeSandbox for this demo:

这是此演示的最终CodeSandbox :

对斯维尔特的好处 ( Benefits to Svelte )

You may be asking why you would use Svelte over something more established like React, Vue, or Angular. This is exactly what I meant to find out in my first experiments.

您可能会问,为什么要对更成熟的东西(如React,Vue或Angular)使用Svelte。 这正是我要在第一个实验中发现的意思。

  • Less code to write: Use HTML/CSS/JS without having to remember too much extra stuff

    更少的代码编写 :使用HTML / CSS / JS无需记住太多额外的东西
  • No Virtual DOM: Svelte compiles your code to tiny, framework-less vanilla JS — your app starts fast and stays fast

    没有虚拟DOM :Svelte将您的代码编译为小型,无框架的原始JS —您的应用程序启动Swift且保持快速
  • Reactive: Reactivity is built-in so you have to worry less about state management

    React式 :React式是内置的,因此您不必担心状态管理

This is all the code that is needed for a Hello World in Svelte:

这是Svelte中的Hello World所需的全部代码:

< script >
    let name = 'world' ;
< / script >

< h1 > Hello { name } ! < / h1 >

You might be thinking, well that's just JavaScript! You're right! The only thing in there that's a little different than vanilla JavaScript is Svelte's templating with name.

您可能在想, 那只是JavaScript ! 你是对的! 唯一与香草JavaScript稍有不同的是Svelte的name模板。

Svelte入门教程和示例 (Svelte Starting Tutorial and Examples)

The Svelte website has a great tutorial walkthrough that you can give a try.

Svelte网站上有很棒的教程演练 ,您可以尝试一下。

如何安装svelte_构建我的第一个Svelte应用程序:想法和印象_第2张图片

There are also some great examples so you can get an idea of what certain apps look like:

也有一些很好的示例,因此您可以了解某些应用程序的外观:

如何安装svelte_构建我的第一个Svelte应用程序:想法和印象_第3张图片

I always like the approach of building an app after going through beginner tutorials. Let's start working on our tiny first app.

在经历了初学者教程之后,我总是喜欢构建应用程序的方法。 让我们开始开发我们的微型第一个应用程序。

构建一个小型的Svelte应用程序 ( Building a Small Svelte App )

The main things that I like to do when trying out a new library/framework is to build an app that:

在尝试新的库/框架时,我喜欢做的主要事情是构建一个应用程序:

  • Grabs data from an API

    从API抓取数据
  • Show data in a list

    在列表中显示数据
  • Create an input box

    创建一个输入框
  • Use that input box to run a search

    使用该输入框运行搜索
  • Take new search data and update our view

    获取新的搜索数据并更新我们的视图

It's a basic app that gives us access to see how the Svelte handles things like:

这是一个基本的应用程序,可让我们访问Svelte如何处理以下内容:

  • HTTP requests

    HTTP请求
  • Lifecycle methods

    生命周期方法
  • Templating - Displaying data

    模板-显示数据
  • Templating - Looping over things

    模板-遍历事物
  • Templating - Conditionally showing things

    模板-有条件地展示事物
  • State - Updating the data and view

    状态-更新数据和视图

Let's grab some data from the GitHub API and show that! Not the most advanced app, but it gets the job done while we are experimenting with Svelte for our first time.

让我们从GitHub API中获取一些数据并进行展示! 这不是最先进的应用程序,但是它是我们第一次尝试Svelte时完成的工作。

GitHub API端点 ( GitHub API Endpoints )

We'll be using the GitHub API to grab some users. The GitHub API is good because it doesn't require us to authenticate for small demo apps like the one we'll be building.

我们将使用GitHub API来吸引一些用户。 GitHub API很不错,因为它不需要我们对将要构建的小型演示应用程序进行身份验证。

We'll also be using the API to search for GitHub users. These are our API endpoints:

我们还将使用API​​搜索GitHub用户。 这些是我们的API端点:

  • Get Users: https://api.github.com/users

    获取用户: https : //api.github.com/users
  • Search Users: https://api.github.com/users/octocat

    搜索用户: https : //api.github.com/users/octocat

You can click each of those links to see what the API will return in JSON.

您可以单击每个链接以查看API将以JSON返回的内容。

如何安装svelte_构建我的第一个Svelte应用程序:想法和印象_第4张图片

I'm using the Chrome Extension called JSON Formatter to make the JSON look good in browser.

我正在使用称为JSON Formatter的Chrome扩展程序,以使JSON在浏览器中看起来不错。

启动我们的Svelte应用程序 ( Starting Our Svelte App )

Let's open up CodeSandbox and create a new Svelte app.

让我们打开CodeSandbox并创建一个新的Svelte应用程序。

如何安装svelte_构建我的第一个Svelte应用程序:想法和印象_第5张图片

Once we click on a Svelte app, we can see the base Svelte app that has a few files. index.js is where everything gets bootstrapped and Rollup is used to compile our Svelte app to vanilla JavaScript.

单击Svelte应用程序后,我们可以看到具有一些文件的基本Svelte应用程序。 index.js引导所有内容,并使用Rollup将Svelte应用程序编译为原始JavaScript。

如何安装svelte_构建我的第一个Svelte应用程序:想法和印象_第6张图片

Notice that there is only one dependency: Svelte.

请注意, 只有一个依赖项 :Svelte。

Our index.js file is where our application gets bootstrapped

我们的应用程序被引导到我们的index.js文件中

// index.js

import App from "./App.svelte" ;

const app = new App ( {
  target : document . body
} ) ;

export default app ;

We can look inside of our App.svelte to see what a Svelte component looks like:

我们可以在App.svelte内部查看Svelte组件的外观:

// App.svelte

< script >
    import Button from "./Button.svelte" ;
< / script >

< style >
  main {
    font - family : sans - serif ;
    text - align : center ;
  }
< / style >

< main >
    < h1 > Hello CodeSandbox < / h1 >
    < h2 > Start editing to see some magic happen ! < / h2 >
    < Button / >
< / main >

A Svelte component consists of:

Svelte组件包括:

  • script: Where all of our JavaScript lives

    script :我们所有JavaScript都存在的地方
  • style: All of our scoped styles live here

    style :我们所有范围内的样式都住在这里
  • Template: Everything outside of a script or style tag will be treated as the template for this component

    模板:脚本或样式标签之外的所有内容都将被视为该组件的模板

Unlike Vue, we don't need a