What's the best way to implement the correct styles for an element-plus message box?

I recently created a minimal page and integrated the Element-Plus message box. However, the appearance of the message box was different from what I expected based on the Element-Plus documentation.

My setup includes Vue, Vite, and ElementPlus. I followed the instructions from the Vite and Element Plus documentation to set up my project. Interestingly, other elements render correctly except for the message box. Here is a snippet of the minimal App.vue component that demonstrates the issue:

<template>
  <el-button text @click="open">Click to open the Message Box</el-button>
</template>

<script setup>
import { ElMessageBox } from 'element-plus'

const open = () => {
  ElMessageBox.alert('This is a message', 'Title', {
    confirmButtonText: 'OK'
  })
}
</script>

Here is my vite.config.js:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    })
  ],
  base: ''
})

The structure of the page is simple:

<!DOCTYPE html>
<title>Vite + Vue</title>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>

And the script:

import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')

Lastly, here is my package.json configuration:

{
  "name": "v2",
  "private": true,
  "version": "0.0.0",
  "main": "main.js",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "start": "electron ."
  },
  "dependencies": {
    "electron": "^20.0.2",
    "element-plus": "^2.2.12",
    "vue": "^3.2.37"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^3.0.2",
    "unplugin-auto-import": "^0.11.1",
    "unplugin-vue-components": "^0.22.4",
    "vite": "^3.0.6"
  }
}

Answer №1

The documentation provided here lacks sufficient explanation

It should be noted that when using ElMessage or ElMessageBox, there may be a need to manually import their styles. The Quick Start / On-demand Import section does not address this issue, leading to some confusion as all other elements seem to work seamlessly out of the box.

<template>
  <el-button text @click="open">Click to open the Message Box</el-button>
</template>

<script setup>
import { ElMessageBox } from 'element-plus';
import 'element-plus/es/components/message/style/css'; // This is necessary only if ElMessage is also used on the page
import 'element-plus/es/components/message-box/style/css';

const open = () => {
  ElMessageBox.alert('This is a message', 'Title', {
    confirmButtonText: 'OK'
  })
}
</script>

Answer №2

When utilizing 'unplugin-auto-import/vite' and 'unplugin-vue-components/vite', importing { ElMessageBox } from 'element-plus' is unnecessary:

import { ElMessageBox } from 'element-plus';

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

Attempting to link the input field with a data value on a Vue page

I'm currently facing an issue with my table and binding the input element to the "project_settings" object. The goal is to have the values inside the project_settings object change based on whether the checkbox is checked or not. However, every time I ...

What is the best way to switch between my two images upon clicking?

When I click on an image, it changes to a different image. I achieved this by altering the source of the image upon clicking. Now, my goal is to incorporate a smooth transition between the two images after they are clicked. I attempted to stack the two im ...

Tutorial on activating Stylus URL rewrites

Is there a way to convert stylus URLs into base64 encoded form? I want to change url('/path/to/img.png') to its base64 encoded version. I have been referencing the documentation here, but it's not providing much help. I attempted to includ ...

Encountering an issue with importing a component in a mixin in NuxtJS

Currently, my main technologies are Nuxtjs and Nuxt-property-decorator To prevent repeating a certain method, I created a mixin This method requires the use of a component (Alert component) In order to use the component in the mixin, I imported it Howe ...

Internet Explorer 11 encountering a flaw with absolute positioning

In my web page, everything looks good in Google Chrome, Firefox, and Opera, but I'm facing an issue with Internet Explorer 11. Here is the simplified HTML: <div class="container"> <div class="page-content"> <div id="corner ...

I am interested in creating some CSS styles to modify the color of a toggle switch's plus sign icon

Can anyone help me customize my Wordpress theme? I'm trying to change the color of my toggle plus signs to white using CSS, but I can't figure it out. Your assistance would be greatly appreciated. Here is the URL: This is the CSS code I attempt ...

Guide on integrating the Google Maps API SDK into a Native project

I recently tried implementing gmap on my web application by following a tutorial I found here: However, I am having trouble understanding how to use the API, specifically when it comes to utilizing a keystore for my API key. Can anyone provide guidance on ...

Overlapping input elements occur when Twitter bootstrap prepended input elements are positioned side by side

I ran into an issue while attempting to place two input elements side by side, with a prefixed item on the first input, using display:table styles. Unfortunately, the second input element ends up overlapping the first. I tried using box-sizing: border-box ...

Detecting resolutions on an iOS Simulator using PhoneGap

As a newcomer to the world of development, I am diving into the process of creating iOS apps using Phonegap. To accommodate different resolutions, I have developed four separate CSS files. <link rel="stylesheet" media="only screen and (max-device-width ...

arranged horizontally in a row html bootstrap

How can I place 2 horizontal cards next to each other? My current code overlaps the cards and creates a messy layout. I want the cards to be side by side like this: https://i.sstatic.net/s6225.png <div class="card-body p-0 d-flex justify-content-arou ...

The Struggle with Bootstrap Accordion

I'm currently in the process of learning bootstrap. I copied the code for the 'Flush' Accordion from the bootstrap docs, but my version looks completely off. Despite linking the CSS stylesheet and JS Bundle, it doesn't seem to align pro ...

The PrimeReact components are not displaying the PrimeReact theme properly

I'm currently working on integrating a Menubar component from PrimeReact into my React application. I tried to apply one of the predefined PrimeReact themes by importing it, but the page ended up looking strange. When I imported "./../../node_modules ...

Validate the provided input against the value stored in the JSON file

Currently utilizing BootstrapVue. I have a scenario where I need to autofill the first b-form-input from a query string. Following that, I am required to enter an ID in another b-form-input and validate if it matches the ID stored in my json file (associa ...

Adjust the color of the contact information when the hamburger menu is activated

I'm currently working on customizing my WordPress site, which uses the Enfold theme. I have implemented a burger menu for all screen widths, and now I want to change the color of the contact information (phone and email) when the burger menu is opened ...

Inject arguments/data into the methods to showcase the name within a newly instantiated component in VueJS

I implemented a feature that allows collapsing sections within a table body. There is an "add" button represented by the "+" sign, and when clicked, it adds a new collapse section with content. I want to display the name of the data row (e.g., "iPhone") wh ...

Reducing image size using CSS can result in blurry images on multiple web browsers

Encountering challenges with downscaled images in multiple browsers... The images must be downscaled to adapt to the browser size. Here is the code snippet: #pic-holder img { -moz-transform: rotate(0deg); /*image-rendering:-webkit-optimize-contras ...

Interactive Image with Markers - Continued

For those of you just starting out, I want to apologize if my question isn't crystal clear. My goal is to create an image that will respond both horizontally and vertically, with pins overlaying the image that adjust in size based on the picture itse ...

Error TS2339: The property 'showNewCustomerFields' is not present on the object of type '{ newCustomer(owner: any): void; }'

As a newcomer to both Vue and Ionic, I am struggling to understand why I keep encountering the TS2339 error. Any assistance on this issue would be greatly valued. data() { return { owner: "default", showNewCustomerFields: false ...

Unable to get div elements to expand to their full width

Trying to center items within a div, but having issues achieving the desired output. This is an image of the desired layout: https://i.sstatic.net/949al.png The items in the div are aligning to the left and the input text does not stretch across ...

When a div is covered by an if statement

One of the challenges I am facing is managing a view with multiple projects, some of which are active while others are not. In my function, if a project's deadline has passed, it displays '0' days left on the view, indicating that these are ...