Getting Started With Vue CLI 3 + TypeScript

Getting Started With Vue CLI 3 + TypeScript

In this tutorial, we’ll be exploring Vue CLI 3, along with the TypeScript, in order to quickly scaffold a project.

Creating a project

Note that the following steps can also be achieved on the Vue GUI — to open it type Vue ui into your command line.

Start off by creating a Vue CLI project as normal:

# Install Vue CLI if it isn't already installed
npm install --global @vue/cli

# Create a new project
vue create my-project-name

When prompted, choose ‘Manually select features’ and then select TypeScript. Select any other features that you would like.

Further options

After this, you’ll be asked to select additional options.

Use class-style component syntax? Vue TypeScript components can be written in either of two ways:

Answer Yes to install the libraries needed to write class-style components.

Pick a linter / formatter config: 

Choose TSLint for appropriate TypeScript syntax checking and analysis.

Wait for the project to be created, and then type npm run serve to see it live.

Project structure

Your folder structure will look similar to a typical Vue CLI project. Some files are specific to TypeScript projects:

Project:
|   babel.config.js
|   package-lock.json
|   package.json
|   postcss.config.js
|   README.md
|   tsconfig.json
|   tslint.json
|   .browserslistrc
|   .gitignore
|   
+---node_modules 
|
+---public
|   |   favicon.ico
|   |   index.html
|   |   manifest.json
|   |   robots.txt
|   |   
|   \---img
|       \---icons
|               
\---src
    |   App.vue
    |   main.ts
    |   registerServiceWorker.ts
    |   router.ts
    |   shims-tsx.d.ts
    |   shims-vue.d.ts
    |   store.ts
    |   
    +---assets
    |       logo.png
    |       
    +---components
    |       HelloWorld.vue
    |       
    \---views
            About.vue
            Home.vue

tsconfig.jsonDenotes a TypeScript project. Contains compiler options and specifies the location of root files.

tslint.jsonOptions and rules for Typescript linting, for TSlint.

main.ts The equivalent of main.js; the entry file for the project.

shims-tsx.d.ts Allows the use of .tsx files. Read more about this here.

shims-vue.d.ts Allows .vue single file components to be imported and used.

Writing components

We can now write TypeScript in .vue single file components, within the script tags.

<script lang="ts"></script>

Components are written using either Vue.extend() or the @Component decorator. Choose the way that is best suited to your needs.

Basic usage, with Vue.extend()

Similar to declaring a normal Vue component. Vue.extend() is used so that the code inside can be subject to type inference.

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
  name: 'HelloWorld',
  props: {
    msg: String,
  },
});
</script>

<style scoped lang="scss">
</style>

Class-style, with @Component decorator

Decorators, such as @Prop, @Watch, @Emit, are used to define the component. 

This is achieved using a combination of two libraries, vue-class-component and vue-property-decorator.

<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;
}
</script>

<style scoped lang="scss">
</style>

IDE support for TypeScript

TypeScript support and features are available with these IDEs:

Top Vue.js Developers in USA

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

We, at HireFullStackDeveloperIndia, implement the right strategic approach to offer a wide variety through customized Vue.js development services to suit your requirements at most competitive prices.

Vue.js is an open-source JavaScript framework that is incredibly progressive and adoptive and majorly used to build a breathtaking user interface. Vue.js is efficient to create advanced web page applications.

Vue.js gets its strength from the flexible JavaScript library to build an enthralling user interface. As the core of Vue.js is concentrated which provides a variety of interactive components for the web and gives real-time implementation. It gives freedom to developers by giving fluidity and eases the integration process with existing projects and other libraries that enables to structure of a highly customizable application.

Vue.js is a scalable framework with a robust in-build stack that can extend itself to operate apps of any proportion. Moreover, vue.js is the best framework to seamlessly create astonishing single-page applications.

Our Vue.js developers have gained tremendous expertise by delivering services to clients worldwide over multiple industries in the area of front-end development. Our adept developers are experts in Vue development and can provide the best value-added user interfaces and web apps.

We assure our clients to have a prime user interface that reaches end-users and target the audience with the exceptional user experience across a variety of devices and platforms. Our expert team of developers serves your business to move ahead on the path of success, where your enterprise can have an advantage over others.

Here are some key benefits that you can avail when you decide to hire vue.js developers in USA from HireFullStackDeveloperIndia:

  • A team of Vue.js developers of your choice
  • 100% guaranteed client satisfaction
  • Integrity and Transparency
  • Free no-obligation quote
  • Portal development solutions
  • Interactive Dashboards over a wide array of devices
  • Vue.js music and video streaming apps
  • Flexible engagement model
  • A free project manager with your team
  • 24*7 communication with your preferred means

If you are looking to hire React Native developers in USA, then choosing HireFullStackDeveloperIndia would be the best as we offer some of the best talents when it comes to Vue.js.