PrimeFlex 保证金在 Vue3 应用程序中不起作用

PrimeFlex margin not working inside Vue3 app

我想在我的 Vue3 应用程序中的 PrimeVue 元素上应用一些边距。基于文档中的示例

https://www.primefaces.org/primevue/showcase/#/selectbutton

我有一个在图标和文本之间有空白的工作示例

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />

        <!-- PrimeVue -->
        <link href="https://unpkg.com/primevue@^3/resources/themes/saga-blue/theme.css" rel="stylesheet" />
        <link href="https://unpkg.com/primevue@^3/resources/primevue.min.css" rel="stylesheet" />
        <link href="https://unpkg.com/primeflex@2.0.0/primeflex.min.css" rel="stylesheet" />
        <link href="https://unpkg.com/primeicons/primeicons.css" rel="stylesheet" />

        <!-- Dependencies -->
        <script src="https://unpkg.com/vue@next"></script>
        <script src="https://unpkg.com/primevue@^3/core/core.js"></script>

        <!-- Demo -->
        <script src="https://unpkg.com/primevue@^3/selectbutton/selectbutton.min.js"></script>
        <link href="./index.css" rel="stylesheet" />
    </head>
    <body>
        <div id="app">
            <p-selectbutton v-model="selectedValue" :options="options" dataKey="value">
                <template #option="slotProps">
                    <i class="pi pi-check-circle" />
                    <span class="p-ml-4">Some text</span>
                </template>
            </p-selectbutton>
        </div>

        <script type="module">
        const { createApp, ref } = Vue;

        const App = {
            setup() {
                const selectedValue = ref();
                
                const options = ref([
                    { value: 'left' },
                    { value: 'right' }
                ]);

                return { selectedValue, options }
            },
            components: {
                "p-selectbutton": primevue.selectbutton
            }
        };

        createApp(App)
            .use(primevue.config.default)
            .mount("#app");
        </script>

    </body>
</html>

但是边距在我的项目中不起作用。复制:

vue create foo

# select default Vue3 app

cd foo

# based on https://primefaces.org/primevue/showcase/#/setup

npm install primevue

npm install primeicons

npm install primeflex

我将 main.js 文件更改为

import { createApp } from 'vue'
import PrimeVue from "primevue/config";
import "primevue/resources/themes/saga-blue/theme.css";
import "primevue/resources/primevue.min.css";
import "primeicons/primeicons.css";
import "primeflex/primeflex.css";
import SelectButton from "primevue/selectbutton";
import App from './App.vue'

createApp(App)
    .use(PrimeVue)
    .component("p-select-button", SelectButton)
    .mount('#app')

我将 App.vue 文件更改为

<template>
  <p-select-button v-model="selectedValue" :options="options" dataKey="value">
      <template #option>
          <i class="pi pi-check-circle" />
          <span class="p-ml-4">Some text</span>
      </template>
  </p-select-button>
</template>

<script>
import { ref } from "vue";

export default {
  setup() {
    const selectedValue = ref();
    
    const options = ref([
        { value: 'left' },
        { value: 'right' }
    ]);

    return { selectedValue, options }
  },
}
</script>

当运行应用程序结果是

有人知道为什么边距在第一个示例中起作用但在我的代码中不起作用吗?我错过了什么?

您的 CDN 演示使用 PrimeFlex 2:

<link href="https://unpkg.com/primeflex@2.0.0/primeflex.min.css" rel="stylesheet" />
                                        

但是您的应用使用 PrimeFlex 3(即,npm install primeflex 安装 3.1.0)。最新版本有removed the p- prefix from all classnames for readability, so the class name is now ml-4.

解决方案 1:将类名更新为 ml-4

您可以在 App.vue 中使用 PrimeFlex 3 的类名:

<!-- BEFORE -->
<span class="p-ml-4">Some text</span>

<!-- AFTER -->
<span class="ml-4">Some text</span>

demo 1

解决方案 2:降级到 PrimeFlex 2

如果您更喜欢使用现有版本(可能是为了尽量减少大型应用程序中的更改),请改为安装 PrimeFlex 2:

npm install -S primeflex@2

demo 2