Is it possible for me to repurpose my current Vue component as a single file component?

After working on a Vue app that was directly written into an HTML file served by Django, I am now transitioning to a dedicated Vue.js project using the CLI. As part of this transition, I want to break apart all the components from my single file and move them into their own Vue files.

My question is whether I can simply create a file named Overview.vue and copy-paste my component code in there as it is, or if I need to make some modifications?

In most examples of single file components that I have seen, there are dedicated blocks for <style>, <template>, and <script>. However, in the component code provided below, the template is inside the component itself. Do I need to change this structure?

Vue.component('overview', {
  delimiters: [ '[[', ']]' ],
  props: ['jobs', 'links'],
  template: `
  <div overview>
  <h3>Overview</h3>
  <table :style="overviewStyle">
  <tr>
  <td>Start Time</td>
  <td>[[ jobs[0].time_start ]]</td>
  </tr>
  </table>
  </div>
  `,
  computed: {
    overviewStyle() {
      return {
        'padding': '8px',
        'width': '100%',
        'display': 'table',
      };
    },
    methods: {
      getStyle (name) {
        switch (name) {
          case 'SUCCESS':
            return {
              'background-color': '#dff0d8',
              'padding': '10px',
              'line-height': '1.42857143',
              'border': '1px solid #C0C0C0',
            }
          case 'IN_PROGRESS':
            return {
              'background-color': '#f4dc42',
              'padding': '10px',
            }
          case 'FAILURE':
            return {
              'background-color': '#f45942',
              'padding': '10px',
              'line-height': '1.42857143',
              'border': '1px solid #C0C0C0',
            }
        };
      },
    },
  });

Answer №1

Indeed, it is necessary to adhere to the format specified for Vue Single File Components.

This method supports 3 sections: <template>, <script>, and <style>.

<template>
  <!-- place your template content here -->
</template>

<script>
  // add JavaScript code here
  export default {
    data() {
      return { ... }
    },
    methods: { ... }
    // and so on
  }
</script>

<style>
  /* incorporate any necessary CSS styles here */
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying the focus icon on the active tab using Angular Material

I am currently utilizing Angular material to dynamically generate tabs in my HTML code. I'm trying to display a drop arrow icon on the active or selected tabs, but I am facing some challenges with the implementation. Below is the code snippet I have ...

Encountering a parse error when making an AJAX call using structural functions

I'm in the process of developing an API and here is my PHP function. function retrieve_schools($cn){ $schools_query = "SELECT * FROM schools"; $school_result = mysqli_query($cn, $schools_query); $response_array['form_data'][&apo ...

Accessing method from child component in parent component in Vue.js is a common requirement that can be easily

Is there a way to access a method in the parent component from the child component? I attempted to use mixins but found that the function was returning null instead of the expected value. However, when emitting an object, it worked fine in the parent compo ...

What is the best method for placing an element above all other elements on a page, regardless of the layout or styles being used?

I want to create a sticky button that remains fixed on the page regardless of its content and styles. The button should always be displayed on top of other elements, without relying on specific z-index values or pre-existing structures. This solution must ...

Display data in a template upon receiving a response from the server

Hello, I am currently in the process of developing my very first Angular application and could use some assistance. Specifically, I am working on a component that serves as an image search box. When a user inputs a search query, a request is sent to an API ...

Does the AngularJS Controller disappear when the DOM element is "deleted"?

One challenge I encountered is regarding a directive that is connected to an angularjs controller, as shown below: <my-directive id="my-unique-directive" ng-controller="MyController"></my-directive> In the controller, at a certain point, I ne ...

Bottom navigation in Vuetify fails to function properly on smaller screens

Utilizing Vuetify's bottom navigation component in my web application, I have implemented multiple sections that require navigation items on the bottom navigation bar. The code snippet below showcases the component being used: <template> < ...

What are the steps for defining the maximum and minimum values in AngularJS?

I'm working with the following HTML markup: <div class="input-group-icon">Max <span class="error">*</span> <div class="input-group"> <input style="border-right:none;" name="available_funds_max" ng-model="attributes.avai ...

React does not allow objects as children, but what if it's actually an array?

My function is encountering an error message that reads: "Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead." I am struggling to understand why this error is occurring. ...

Is there a way to switch the classList between various buttons using a single JavaScript function?

I'm currently developing a straightforward add to cart container that also has the ability to toggle between different product sizes. However, I am facing difficulties in achieving this functionality without having to create separate functions for ea ...

Leveraging v-for within a component that incorporates the <slot/> element to generate a versatile and interactive Tab menu

Currently, I am in the process of developing a dynamic tab menu using Vue 3 and slots. Successfully implementing tabs, I have created BaseTabsWrapper and BaseTab components. The challenge lies in using v-for with the BaseTab component inside a BaseTabsWrap ...

The next and previous buttons on the JQuery UI datepicker are related to only one ancestor. When using $(e.target).parents(), it will only return a

Unfortunately, my reputation isn't high enough to post my own answer just yet. However, I wanted to share it before wasting more people's time: I finally figured out why I was not getting all the expected ancestors: The JQuery datepicker actuall ...

In Node.js and Express, it is important to note that a variable must be declared before

When I use the express action get('items'), I encounter an issue while trying to call an external JSON-API and display the API response in my local response. The error that I am currently facing states that the items variable is not defined with ...

Tips for clearing Nightwatch session storage efficiently

To ensure the pop-up functionality was working properly, I had to reset the browser's session storage. By doing this, the pop-up will reappear. Is there a way to clear the page's Session Storage in Nightwatch? ...

TypeScript combined with Vue 3: Uncaught ReferenceError - variable has not been declared

At the start of my <script>, I define a variable with type any. Later on, within the same script, I reference this variable in one of my methods. Strangely, although my IDE does not raise any complaints, a runtime error occurs in my console: Referenc ...

Directive containing tracking code script

I'm working on creating a directive to dynamically include tracking code scripts in my main page: <trackingcode trackingcodevalue="{{padCtrl.trackingnumber}}"></trackingcode> This is the directive I have set up: (function () { 'use ...

What is the best way to manage a group of checkboxes using EJS?

I am currently utilizing ejs for my website pages and on one particular page, I have an array of objects. The number of objects in the array is not known when the page initially loads. This page allows me to edit a series of announcements and I would like ...

Retrieve the nearest attribute from a radio button when using JQuery on a click event

I have a query regarding the usage of JS / JQuery to extract the data-product-name from a selected radio button, prior to any form submission. Although my attempt at code implementation seems incorrect: return jQuery({{Click Element}}).closest("form" ...

Strategies for Synchronizing Multiple Asynchronous Calls in NodeJS

I have a function getAliasesByRoleDetailed(role) that is responsible for retrieving user data based on a given role. This particular function utilizes axios to fetch the necessary data. The result obtained from executing this function appears in the follo ...

Using a ThreeJS bitmap texture to extrude a shape created with THREE.Shape

I have successfully created a cube with rounded corners using the THREE.Shape object: var shape = new THREE.Shape(); x = width/2; y = height/2; shape.moveTo(x - radius, y); //*** Top right x = -width/2; y = height/2; shape.lineTo(x + radius, y); if (tr) ...