覆盖动态地修改 Vuetify 类

huangapple go评论71阅读模式
英文:

Override Vuetify Class Dynamically

问题

我有一个 Vuetify 对话框,我想要动态地设置样式。问题在于样式需要覆盖默认的 v-dialog 样式。出于某种原因,当使用计算样式绑定、CSS 类中的变量,甚至直接输入样式时,都不会对样式产生影响。如何在 Vuetify 组件上动态覆盖默认样式?

这是我的对话框代码:

<template>
  <v-dialog 
    v-model="dialog" 
    max-width="350px"
    persistent
    hide-overlay
    style="{position: absolute; bottom: 0px;}"
  >        
    <div class="arrow-up mb-n1 ml-1"></div>
    <v-card>
      <v-container>
        <v-card-text>
          <v-row>
            <v-col 
              cols="12"
              align="start"
            >
              <p class="text-subtitle-1">
                最好从头开始,让我们先获取您的产品。
              </p>
            </v-col>
          </v-row>
        </v-card-text>
        
        <v-card-actions class="mt-n8">
          <v-btn
            color="#68007d"
            text
            @click="goToImportMethod"
          >
            下一步
          </v-btn>
          <v-btn
            color="#616161"
            text
            @click="closeEverything"
          >
            关闭
          </v-btn>
        </v-card-actions>
      </v-container>
    </v-card>
  </v-dialog>
</template>

我尝试使用如下的计算属性:

computed: {
  myStyle() {
    return {
      bottom: '0px',
      left: '150px',
      position: 'absolute'
    }
  }
},

然后在 v-dialog 中使用它:

<v-dialog 
  v-model="dialog" 
  max-width="350px"
  persistent
  hide-overlay
  :style="myStyle"
>

我还尝试在类本身中添加变量,像这样:

.v-dialog {
  box-shadow: none;
  overflow: visible;
  bottom: var(--bottom);
  left: 150px;
  position: absolute;
}

并且使用一个简单的数据属性,例如:

data () {
  return {
    dialog: true,
    bottom: '0px',
  }
},

但最终唯一有效的方法是静态地覆盖类,这对我没有好处:

.v-dialog {
  box-shadow: none;
  overflow: visible;
  bottom: 0px;
  left: 150px;
  position: absolute;
}
英文:

I have a Vuetify dialog that I want to style dynamically. The issue is that the stylings need to overwrite the default v-dialog stylings. For some reason when using computed style bindings, variables in the CSS class, or even just directly inputting the styles, makes no impact to the style. How can I dynamically overwrite default styling on a Vuetify component?

Here is my dialog:

&lt;template&gt;
  &lt;v-dialog 
    v-model=&quot;dialog&quot; 
    max-width=&quot;350px&quot;
    persistent
    hide-overlay
    style=&quot;{position: absolute; bottom: 0px;}&quot;
  &gt;        
  &lt;div class=&quot;arrow-up mb-n1 ml-1&quot;&gt;&lt;/div&gt;
  &lt;v-card&gt;
    &lt;v-container&gt;
    &lt;v-card-text&gt;
    &lt;v-row&gt;
      &lt;v-col 
        cols=&quot;12&quot;
        align=&quot;start&quot;
      &gt;
        &lt;p class=&quot;text-subtitle-1&quot;&gt;
        Always best to start at the beginning, let&#39;s pull your products first.&lt;/p&gt;
      &lt;/v-col&gt;
    &lt;/v-row&gt;
    &lt;/v-card-text&gt;
      
      &lt;v-card-actions class=&quot;mt-n8&quot;&gt;
        &lt;v-btn
          color=&quot;#68007d&quot;
          text
          @click=&quot;goToImportMethod&quot;
        &gt;
          NEXT
        &lt;/v-btn&gt;
        &lt;v-btn
          color=&quot;#616161&quot;
          text
          @click=&quot;closeEverything&quot;
        &gt;
          CLOSE
        &lt;/v-btn&gt;
      &lt;/v-card-actions&gt;
    &lt;/v-container&gt;
  &lt;/v-card&gt;
  &lt;/v-dialog&gt;
&lt;/template&gt;

and I've tried using a computed property like this:

  &lt;v-dialog 
    v-model=&quot;dialog&quot; 
    max-width=&quot;350px&quot;
    persistent
    hide-overlay
    :style=&quot;myStyle&quot;
  &gt;

With this:

computed: {
  myStyle() {
    return {
      bottom: &#39;0px&#39;,
      left: &#39;150px&#39;,
      position: &#39;absolute&#39;
    }
  }
},

I then tried adding in variables inside the class itself like this:

&gt;&gt;&gt; .v-dialog {
  box-shadow: none;
  overflow: visible;
  bottom: var(--bottom);
  left: 150px;
  position: absolute;
}

with a simple data property like this:

data () {
  return {
    dialog: true,
    bottom: &#39;0px&#39;,
  }
},

But in the end the only thing that works is statically overwriting the class which does me no good:

&gt;&gt;&gt; .v-dialog {
  box-shadow: none;
  overflow: visible;
  bottom: 0px;
  left: 150px;
  position: absolute;
}

答案1

得分: 2

The v-dialog创建一个包含实际对话框的覆盖层。如果您想传递像 :style 这样的 props 到容器,您可以使用 :content-props 属性:

&lt;v-dialog
  :content-props=&quot;{style: myStyle}
  ...

如果您只想将 CSS 类传递给容器,您可以使用 :content-class 属性。

看一下这个片段:

<!-- 开始片段: js 隐藏: true 控制台: false babel: false -->

<!-- 语言: lang-js -->

const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
setup(){
const styles = {
bottom: '0px',
left: '0px',
position: 'absolute',
}

return {
  dialog: ref(true),
  styles
}

}

}
createApp(app).use(vuetify).mount('#app')

<!-- 语言: lang-html -->

<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css" />
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-main>
<v-dialog v-model="dialog" max-width="350px" persistent hide-overlay :content-props="{style: styles}">
<div class="arrow-up mb-n1 ml-1"></div>
<v-card>
<v-container>
<v-card-text>
<v-row>
<v-col cols="12" align="start">
<p class="text-subtitle-1">
Dialog at bottom 0 and left 0.</p>
</v-col>
</v-row>
</v-card-text>

      &lt;/v-container&gt;
    &lt;/v-card&gt;
  &lt;/v-dialog&gt;
&lt;/v-main&gt;

</v-app>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js"></script>

<!-- 结束片段 -->

英文:

The v-dialog creates an overlay which contains the actual dialog. If you want to pass props like :style to the container, you can use the :content-props property:

&lt;v-dialog
  :content-props=&quot;{style: myStyle}
  ...

If you just want to send CSS classes to the container, you can use the :content-class property.

Have a look at the snippet:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
  setup(){
    const styles = {
      bottom: &#39;0px&#39;,
      left: &#39;0px&#39;,
      position: &#39;absolute&#39;,
    }
    
    return {
      dialog: ref(true),
      styles
    }
  }

}
createApp(app).use(vuetify).mount(&#39;#app&#39;)

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css&quot; /&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;div id=&quot;app&quot;&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;
      &lt;v-dialog v-model=&quot;dialog&quot; max-width=&quot;350px&quot; persistent hide-overlay :content-props=&quot;{style: styles}&quot;&gt;
        &lt;div class=&quot;arrow-up mb-n1 ml-1&quot;&gt;&lt;/div&gt;
        &lt;v-card&gt;
          &lt;v-container&gt;
            &lt;v-card-text&gt;
              &lt;v-row&gt;
                &lt;v-col cols=&quot;12&quot; align=&quot;start&quot;&gt;
                  &lt;p class=&quot;text-subtitle-1&quot;&gt;
                    Dialog at bottom 0 and left 0.&lt;/p&gt;
                &lt;/v-col&gt;
              &lt;/v-row&gt;
            &lt;/v-card-text&gt;

          &lt;/v-container&gt;
        &lt;/v-card&gt;
      &lt;/v-dialog&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月31日 23:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定