英文:
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:
<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">
        Always best to start at the beginning, let's pull your products first.</p>
      </v-col>
    </v-row>
    </v-card-text>
      
      <v-card-actions class="mt-n8">
        <v-btn
          color="#68007d"
          text
          @click="goToImportMethod"
        >
          NEXT
        </v-btn>
        <v-btn
          color="#616161"
          text
          @click="closeEverything"
        >
          CLOSE
        </v-btn>
      </v-card-actions>
    </v-container>
  </v-card>
  </v-dialog>
</template>
and I've tried using a computed property like this:
  <v-dialog 
    v-model="dialog" 
    max-width="350px"
    persistent
    hide-overlay
    :style="myStyle"
  >
With this:
computed: {
  myStyle() {
    return {
      bottom: '0px',
      left: '150px',
      position: 'absolute'
    }
  }
},
I then tried adding in variables inside the class itself like this:
>>> .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: '0px',
  }
},
But in the end the only thing that works is statically overwriting the class which does me no good:
>>> .v-dialog {
  box-shadow: none;
  overflow: visible;
  bottom: 0px;
  left: 150px;
  position: absolute;
}
答案1
得分: 2
The v-dialog创建一个包含实际对话框的覆盖层。如果您想传递像 :style 这样的 props 到容器,您可以使用 :content-props 属性:
<v-dialog
  :content-props="{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>
      </v-container>
    </v-card>
  </v-dialog>
</v-main>
</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:
<v-dialog
  :content-props="{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: '0px',
      left: '0px',
      position: 'absolute',
    }
    
    return {
      dialog: ref(true),
      styles
    }
  }
}
createApp(app).use(vuetify).mount('#app')
<!-- language: 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>
          </v-container>
        </v-card>
      </v-dialog>
    </v-main>
  </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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论