DEV Community

Cover image for Vue - Document Syntax
Beey
Beey

Posted on

Vue - Document Syntax

Table of Contents

What vue is

Vue is an interesting programming language that combines CSS, JS, and HTML into one file it can even use pug, react jsx and tsx, and scss.

Syntax

Every programming language has syntax including vue, this post will teach you how to make a basic vue document.

More Information

You can visit vue tutorial for more information.

Required Elements

Every vue document must contain atleast one <script> or <style> element, if not done vue will throw an exception.

The script element

A <script> element contains the javascript however with the lang property it can be switched to typescript, react JSX, and react TSX.

You can manualy set lang to JS aswell.

the setup property allows you to use ref() and reactive() without manualy using the setup() function.

An example of a basic <script>:


<script lang="js">
  const message = "Hello World";

  console.log(message);
</script>

Enter fullscreen mode Exit fullscreen mode

The template element

Another important vue element is <template> which contains HTML or pug.

Template also supports the lang attribute.

A basic example:


<template>
  <header>
    <h1>My Header</h1>
  </header>
 </template> <!-- the lang attribute is not required in elements -->

Enter fullscreen mode Exit fullscreen mode

The style element

The <style> element is where CSS or SCSS is in the document.

Like every other discussed element, style has the lang attribute.

The scoped attribute when given causes the style element local to the document and not global.

The module attribute enables CSS modules which also causes the style element to be local and exposes them as an object to the component.

An example of using style would be:


<style lang="css" scoped>
  .MyClass {
    color: red;
  }
</style>

Enter fullscreen mode Exit fullscreen mode

An example vue document

Here's an example of a vue document:


<script lang="js">
  const Message = "Hello World";

  console.log(Message);
</script>
<template lang="html">
  <header>
    <h1 class="MyClass">My Heading</h1>
  </header>
</template>
<style lang="css" scoped>
  .MyClass {
    color = red;
  }
</style>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)