"Upon completion of the build process, the Vue.js Vite application that was operating smoothly during

I recently created a website using vite and vue.js. Initially, everything worked perfectly when I ran npm run dev. However, after building the app with npm run build, it stopped functioning properly. A blank page appeared with an error message stating "cannot read property of undefined (reading isDark)". See the error message here.

Here is the relevant snippet from my app.vue code. The 'isDark' property is only used in this component:

<script setup>
// This starter template is based on Vue 3 <script setup> SFCs
// For more information, visit https://vuejs.org/api/sfc-script-setup.html#script-setup
import Header from "./components/Header.vue";
import Footer from "./components/Footer.vue";
</script>

<template>
  <div :class="'portfolio  ' + (this.isDark ? 'dark' : '')">
    <Header
      @toggleDarkMode="() => toggleDarkMode()"
      :dark="this ? this.isDark : false"
    />
    <div
      class="
        content
        transition-colors
        duration-300
        ease-linear
        text-black
        dark:text-white
        bg-gray-300
        dark:bg-gray-800
        pt-16
      "
    >
      <router-view />
    </div>
    <Footer />
  </div>
</template>

<script>
export default {
  name: "App",
  emits: ["toggleDarkMode"],
  beforeCreate() {
    console.log(this.isDark);
    console.log(this.isDark != null);
    this.isDark = localStorage.getItem("darkMode") == "true";
    console.log(this.isDark);
  },
  created() {
    this.isDark = localStorage.getItem("darkMode") == "true";
  },
  methods: {
    toggleDarkMode() {
      if (this === undefinded) return;
      this.isDark = !this.isDark;
      localStorage.setItem("darkMode", this.isDark);
    },
  },
  data() {
    return {
      isDark: true,
    };
  },
  components: [Header],
};
</script>

<style scoped>
</style>

Answer №1

Implement the use of isDark in your template without using this, as shown below:

<Header
  @toggleDarkMode="toggleDarkMode"
  :dark="isDark"
/>

Alternatively, you can do it like this:

<Header
  @toggleDarkMode="toggleDarkMode"
  :dark="!!isDark"
/>

Remember, there is no need for an anonymous function in the event handler.

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

Implementing Ext JS Checkbox Inside a Row Editor

Does anyone have a solution for aligning the checkbox in a RowEditor to the center position? I am using the "triton" theme with Ext JS 6.0.0. The issue is that the checkbox is currently placed at the top of the row, while other fields like textfield or co ...

Determine if the user has clicked on the Save or Cancel button within the print dialog box

Hello everyone, Can anyone help me figure out how to determine which button was selected by the user in a print dialog box? Thank you! ...

Using the map method in ReactJS to iterate over each item in an array from state, I encountered an issue where the item's property was found to

Transitioning from Python/Django to ReactJS has been an exciting challenge for me. After following a tutorial, I gained a basic understanding of how ReactJS functions. To further my learning, I decided to create a simple scenario where all items stored in ...

Automatically update the border sample once the visitor enters a color code into the input text field

Is it possible to automatically change the sample border when a visitor enters a color code in the input textfield at: Do you have a specific border color in mind? input name="ContentInclude:borderspecs" type="text" maxlength="200" id="ContentInclude_bor ...

Concerns with the window's onload function

window.onload = prepareButton; function prepareButton() { document.getElementById('slist').onclick = function() { window.location = "students"; } } Whenever I click on a slist element, which is actually a <p> tag formatt ...

What is the best way to retrieve the file name from the current document's URL using JavaScript?

I need help with a Javascript function that can extract the current file name without any parameters. $(location).attr('href').match(/([a-zA-Z\-\_0-9]+\.\w+)$/); var current_path = RegExp.$1; if ((current_path == 'index. ...

Using Express to Deliver Static Content to Subdirectories

I am currently using Express to serve a React application that was bootstrapped with Create React App. The project has the following directory structure: |--client/ | |--build/ | | |--static/ | | | |--main.css | | | |--main.js | ...

Mastering event listening in Nodejs: Best practices for handling events between objects

Currently facing the challenge of having an object that needs to listen on another object. The dilemma is: what is the best approach for handling this subscription? My current understanding suggests two possible methods: Using a registrator: var Registra ...

When navigating to '/blogs/', the index.js file in Next.js will automatically open

I'm working on a project using next.js and I want to ensure that when I visit 'localhost:3000/blogs/', it opens the index.js page. The index.js file is located in the 'blogs' folder of my project. Currently, it does open properly ...

Trigger a JavaScript function upon the playing of an audio file

Is there a way to trigger a javascript function whenever an HTML5 audio file is played? And can the same be done for videos as well? ...

"Disabling a FormControl within a FormArray in Angular 4: A Step-by-

I've come up with a code snippet below that I thought would disable the FormControl in a FormArray. some.component.html <form [formGroup]="testForm"> <div *ngFor="let num of countArr"> <input type="text" formNameArray="arr ...

React-Redux Project: Issue with React components not displaying properly, only the Parent directory gets rendered

Luckily, I managed to resolve the webpack build issues successfully. Nevertheless, every time I execute 'npm start' (webpack-dev-server), it serves the parent directory of this file. The reason behind this behavior remains unclear. -I've cr ...

Synchronously executing Twitter posts

Hello there! I've been using this code found here to embed Twitter posts on my website. It's been working perfectly on certain pages, like in the forums, but I've run into an issue while browsing through user profiles. The post history for e ...

Display HTML content for a particular division when clicked

I've implemented some HTML and Ajax where clicking a specific image (with the reply-quote class below) triggers an Ajax request to display HTML elsewhere on the page. The issue arises from having multiple instances of the same block of divs within th ...

A guide on efficiently updating multiple Vue 3 components when a Pinia array undergoes changes

I'm facing an issue where multiple components accessing the same store don't respond to updates unless I manipulate a DOM element to trigger a new render. Within my Pinia store, I have an array and an update method: let MyArray: IMyItem[] = ...

how to manipulate checkbox selection and deselection with reactive forms in Angular 8

Hey there! I'm currently working with an array of objects that contain IDs and values. I'm using reactive forms to bind these objects to HTML, but when I try to select and deselect all, I'm not getting the desired output without any errors. ...

Can you explain NodeSource in simple terms, and what purpose does it serve?

Not too long ago, I delved into researching how to effectively host a MEAN stack web application on AWS. During my quest for knowledge, I stumbled upon a tutorial that caught my eye. The one I ended up following can be found at https://www.youtube.com/wat ...

Utilize Chrome storage instead of localstorage to generate Parse sessions

I'm currently developing a Chrome Extension that relies on Parse User sessions. Because localstorage is limited to specific domains, I am looking to utilize chrome.storage so the data can be accessed across any site. The existing Parse Javascript SDK ...

What occurs when multiple HTML tags are assigned the same string as their ID attribute value?

While browsing a html page, I stumbled upon several tags that I thought had the same id. It turns out they were unique, but it got me thinking - what would happen if multiple tags actually shared the same id? I've always heard that the id attribute o ...

I'm struggling to understand how to enable slash commands for my dice command

I'm working on creating a dice slash command with a command handler that displays an image of the dice, but I keep encountering the issue where it says url1 is undefined. Also, when attempting to directly post the images, I receive an error message st ...