What is the best way to structure Vue.js components for optimal organization?

Imagine having an index page called index.vue consisting of various components like this:

index.vue

<template>
<div>
  <component-1 />
  <section class="section-1">
    <div class="container section-container">
      <component-2 />
      <component-3 /> 
    </div>
  </section>
  <component-4>
</div>
</template>

import Component-1 from '~/components/Component-1'
import Component-2 from '~/components/Component-2'
import Component-3 from '~/components/Component-3'
import Component-4 from '~/components/Component-4'


export default {
  components: {
      Component-1
      Component-2
      Component-3
      Component-4
  }
}

<style>
// style for the index page
</style>

Furthermore, there are files named component-2.vue and component-3.vue where specific css rules are declared. In this scenario, certain elements within these components have unique classes while others do not. If styling, let's say, section-1 is defined in the index.vue file, those styles will be inherited by all its children components. This setup allows for styling unique classes in the component-1.vue and component-2.vue files (using <style scoped>) while styling non-unique classes like section-1, container, section-container inside index.vue. Even though section-1, container, section-container exist in both component-1.vue and component-2.vue.

Thus arises the question: Is it a reasonable practice to structure code in this manner? This is what I seek to comprehend. One drawback of this structure is that css rules for certain elements in component-2 and component-3 are placed outside their respective components.

Answer №1

To ensure proper namespacing of elements in your Vue app, utilize the <style scoped> tag. In my experience working with a complex Vue application, I have found it useful to either incorporate unscoped styles directly into parent components or utilize SCSS or CSS modules within ./src/scss/ for styling shared elements across multiple components. This approach allows you to benefit from Vue's scoped styles functionality while also creating more reusable and modular styles as needed.

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

Aligning Content in the Header

Currently, I am attempting to place a logo on my WordPress site right at the top of the header above the menus and in the center. Even though I've positioned it at the top, I'm struggling to align it horizontally in the center. I've experime ...

The onChange event for React select is being triggered twice, incorrectly using the old value the second time

I am currently implementing React Select to display a list of items. When an item is changed, I need to show a warning based on a specific flag. If the flag is true, a dialog box will be shown and upon confirmation, the change should be allowed. After each ...

Reduce the combined character value of the title and excerpt by trimming the excess characters

I am seeking a solution to trim the total character count of a post title and excerpt combined. Currently, I can individually adjust the excerpt through theme settings and the post title using JavaScript. However, due to varying title lengths, the height ...

What is the best way to locate the key with the greatest value in an array that is less than or equal to a certain

In my data structure, I have an array that maps user levels to the minimum points required for each level. Here's how it looks: $userLevels = array(0 => 0, 1 => 400, 2 => 800); The keys represent user levels and the values represent the min ...

Personalize the error message for throwing an exception in JavaScript

I've been working on customizing error messages for exceptions thrown in JavaScript. Despite my best efforts, I have not been successful so far. I'm currently attempting the following code snippet but it's not functioning as expected: f ...

Is there a way to determine the quantity of child objects and transmit the calculated index to each individual child object?

My data is structured as shown below: team1 : { author92 : "John" , author43 : "Smith" }, team2 : { author33 : "Dolly", author23 : "Mark" }, I want to display Authors grouped together with an ad ...

Angular's UI router is directing users to the incorrect URL

Just starting out with Angular 1 and tasked with adding a new feature to an existing webapp. The webapp utilizes jhipster for backend and frontend generation (Angular 1 and uirouter). I attempted to create my own route and state by borrowing from existing ...

Tips for verifying a login modal on an asp.net webforms using jQuery?

I am currently working on an asp.net webpage that utilizes a modal bootstrap for user login. Upon clicking the Login button, it should authenticate the user and initiate a server-side method called "ExportToZip" to download a zip file. My issue lies in ens ...

An error occurred while trying to access the object null in React-Native, specifically when evaluating 'this.state.token'

I encountered an error while trying to execute a class in a view: When running the class, I received the following error message: null is not an object (evaluating 'this.state.token') The problematic class that I'm attempting to render: c ...

Transforming varied JavaScript objects into a serial form

In my application, there is a concept of an interface along with multiple objects that implement this interface in various ways. These objects are created using different factory methods, with the potential for more factories to be added as the application ...

Obtaining the referring URL after being redirected from one webpage to another

I have multiple pages redirecting to dev.php using a PHP header. I am curious about the source of the redirection. <?php header(Location: dev.php); ?> I attempted to use <?php print "You entered using a link on ".$_SERVER["HTTP_REFERER"]; ?> ...

Issue with PHP's ng-click not functioning properly with Angular JS's dynamic HTML generation

I'm just starting out with Angular JS. I'm trying to create an ng-click event in dynamically generated HTML from PHP, here's the code snippet. var app = angular.module('pageapp',[]); angular.module('pageapp') .filter(& ...

Creating a JavaScript function triggered by a click event that will redirect to

I'm attempting to create an onclick function that will redirect you to a new page when you click on a div element, much like the A + href attribute in HTML. Any suggestions on how to achieve this? Should I just add an A tag inside the div? I'm un ...

Can I confidently utilize this amazing vueJS component for production purposes?

One of the strengths of Vue.js is its rich ecosystem, offering a plethora of free utilities, tools, and components. A particularly intriguing resource is the curated list of Vue components available for free in the "awesome-vue" repository on GitHub. My i ...

Node error connecting to Mongo database

Having trouble connecting my node server to MongoDB, here is the code snippet I am using: var http = require("http"); var url = require("url"); var Router = require('node-simple-router'); var router = Router(); var qs = require('querystring ...

Hover events in the browser are currently operating at a sluggish pace

When I hover over a photo, an overlay is supposed to appear. However, if I move the mouse quickly (from the top to the bottom of the page, for example), the overlay remains visible even after I stop hovering over the photo. I have tried different event ha ...

Error in JavaScript: A surprise anonymous System.register call occurred

Within Visual Studio 2015, there exists a TypeScript project featuring two distinct TypeScript files: foo.ts export class Foo { bar(): string { return "hello"; } } app.ts /// <reference path="foo.ts"/> import {Foo} from './f ...

Adding an external JavaScript library file to a specific component in Angular 7 is a straightforward process. Let's walk through the

As a beginner in Angular framework, I've encountered an issue while working on creating a custom HTML template using Angular version 7. My template consists of various pages like home, about, product, etc. Specifically, on the home page, I am trying t ...

The JavaScript code malfunctions when I introduce a new HTML tag

I am attempting to create a simple photo gallery using PhotoSwipe. However, I have encountered an issue where certain functions like Previous and Next buttons stop working when I add new HTML elements. Here is the original code: <div class="my-gallery ...

Implement CSRF protection for wicket ajax requests by adding the necessary header

I'm currently working on a website created with Apache Wicket and we're looking to enhance its security by implementing CSRF protection. Our goal is to keep it stateless by using a double submit pattern. For forms, we are planning to include a h ...