搜索

首页  >  问答  >  正文

如何应用正确的元素加消息框样式?

这是元素加消息框在我构建的最小页面上的外观:

我希望它看起来像 element-plus 文档中的那样。

我将VueviteElementPlus一起使用。我从 vite 和 element plus 文档复制了设置。我玩了很多其他元素,它们都渲染正确。可以重现问题的最小 App.vue 组件:

<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>

我的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'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    })
  ],
  base: ''
})

页面很小:

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

脚本也是如此:

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

最后是我的 package.json:

{
  "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"
  }
}


P粉052724364P粉052724364428 天前750

全部回复(2)我来回复

  • P粉018653751

    P粉0186537512023-11-24 00:19:03

    如果您使用了“unplugin-auto-import/vite”和“unplugin-vue-components/vite”,则不需要:

    回复
    0
  • P粉833546953

    P粉8335469532023-11-24 00:12:22

    我想说文档解释得不够好 https://element -plus.org/en-US/guide/quickstart.html

    当使用 ElMessageElMessageBox 时,它们的样式可能需要手动导入。 快速启动/按需导入部分没有对此做任何说明,并且所有其他元素都可以开箱即用,因此有点令人困惑。

    <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 only needed if the page also used ElMessage
    import 'element-plus/es/components/message-box/style/css';
    
    const open = () => {
      ElMessageBox.alert('This is a message', 'Title', {
        confirmButtonText: 'OK'
      })
    }
    </script>
    

    回复
    0
  • 取消回复