Use a declarative approach to describe how your component should be rendered.
In the previous section, we created a basic view model, and you are probably sceptical that we are now going to create a view with a text input that updates this value without writing any more Javascript.
Inside the src
directory create a new file called hello-name.html
this will complicate the view model you already have (hello-name.ts
or hello-name.js
).
Firstly, let's write the code to display the value. Notice how we are using interpolation to print the value like the default generated my-app.html
file was? ${name}
the value inside the curly braces references the class property we defined in the previous section, by the name of name
.
I would say run the app and see it in action, but we haven't imported our custom element just yet. Let's do that now.
Inside of my-app.html
replace the entire file with the following:
We use the import
element to include our newly created component. Take note there is a lack of file extension. This is because Aurelia will know to include our view-model and, in turn, include our view and any styles.
We then reference our custom element by its name. Aurelia knows using default conventions to take the file name, strip the file extension and use that as the tag name (this is configurable but beyond the scope of this tutorial).
If you were to run the app using npm start
you would then see your application rendering your new component. You should see the text, Hello, Person!
rendering in the view. Once you have the app running, leave it running as any changes you make will automatically be detected and re-rendered.
We have a functional custom element, but we promised we would be able to update the name with any value we want. Inside of hello-name.html
add the following beneath the existing heading.
You should now see a text input with the value Person
inside of it. By adding value.bind
to the input, Aurelia will use two-way binding by default. This means it will show the value from the view-model inside of the view, and if it changes in either view or view-model, it will be updated. Think of a two-way binding as syncing the value.
Type something into the text input and you should see the heading value change as you type. This works because Aurelia binds to the native value
property of the text input and keeps track of the changes for you without needing to use callbacks or anything else.
As you might have noticed, there really is not that much code here. Leveraging Aurelia's conventions and defaults allows you to write Aurelia applications without the verbosity and without writing tens of lines of code to do things like binding or printing values.
Learn the basics of Aurelia by building an interactive Hello, World! application
This guide will take you through creating a hello world app using Aurelia and briefly explain its main concepts. We will be building a simple hello, world example with a text field you can enter a name into and watch the view update. We assume you are familiar with JavaScript, HTML, and CSS.
Here's what you'll learn...
How to set up a new Aurelia project.
Creating components from view-models and views.
The basics of templating and binding.
Where to go from here.
When you complete this guide, you'll be ready to take your first steps building your own Aurelia app and, you'll have the resources you need to continue into more advanced topics. So, what are you waiting for? Let's get started!
A view-model is where your business logic will live for your components. Follow along as you create your first view-model.
Aurelia as a default convention works on the premise of a view and view-model, both of which are tied together. The view-model is where your business logic lives and, if you have ever worked with .NET before (or other frameworks), you might refer to this as the controller.
Navigate to the src
directory where your application code lives and open up the my-app(.ts/.js
file. This is the main entry point for the application and this file is where business logic would live. As you can see, there is not a lot going on at the moment.
The class property message
contains a string and within our view, we are displaying it using interpolation.
We are now going to create a new component which will be a smarter hello component. Inside of src
create a new file called hello-name
and use the appropriate file extension depending on whether you are using TypeScript or not. So, hello-name.ts
or hello-name.js
.
Notice how our new component doesn't look much different than the generated one? That's intentional. We are going to bind to this name property using two-way
binding from within our view. We don't need any callback functions to update this value either.
Nice one! You just created a custom element view-model. It might not look like much, but you just learned a very core fundamental concept of building Aurelia applications. A view-model can be as simple and complex as you want it to be.
In the next chapter, we will hook this up with our view and allow a text input to change this value.
Understand what areas of the framework to learn next, and how to proceed from here on out.
In our hello world example we touched upon some very basic and light Aurelia concepts. The most important of those is the view/view-model relationship. However, please note that you can write Aurelia applications in a wide variety of different ways, including custom elements without view models.
It is highly recommended that you have a read through additional sections in the "Getting Started" section to become familiar with the templating syntax, building components with bindable properties, looping through collections and more.
Once you are familiar with Aurelia's templating syntax and conventions, you will discover that you end up applying these templating basics to every app that you build.
If you followed along with our tutorial, you would now have a functional hello world application. The application will function like the one below. Typing into the text input field should dynamically display the value you entered.
Learn to use Aurelia's project scaffold tooling to create your first project setup.
There are various ways that you can set up an Aurelia project, including everything from adding a simple script tag to your HTML page to creating a custom Webpack configuration. One of the easiest and most powerful ways to get started is by using the makes
tool.
Before you run makes
, you will need a recent version of Node.js installed on your machine. If you do not have Node.js, you can get it here. Please ensure that Node.js is up-to-date if you already have it installed.
Next, using npx
, a tool distributed with Node.js, we'll create a new Aurelia app. Open a command prompt and run the following command:
makes
will then start the Aurelia project wizard, asking you a few questions to help you get things set up properly. When prompted, give your project the name "hello-world" and then select a default setup, either ESNext (Javascript) or TypeScript, depending on your preference. Finally, choose "yes" to install the project dependencies.
You have now created a hello world app without writing a single line code, well done. However, we'll be updating our app to allow for text input so we can make it say, "Hello" to whoever we want in the next section.
You now have an Aurelia setup ready for you to run, debug, or deploy. To ensure that everything is working properly, cd
into your project folder and run npm start
. Your project will then build and a web browser will open, displaying the message "Hello World".
Congratulations! You just ran your first Aurelia app. Now, let's get building.