Vuetify/VueJS - md/lg/xl 的多个条件
Vuetify/VueJS - Multiple conditions on md/lg/xl
我在 Vue/Vuetify
中的断点上应用条件时遇到困难
基本上我想说...如果数组的长度能被2整除,那么MD=6,如果能被3整除那么MD=12
<v-col cols="12"
:md="module.plannings.length % 2 === 0 ? 6 ? module.plannings.length % 2 === 0 ? 12 : 12"
v-for="planning in module.plannings"
:key="planning.id"
>
很明显,我在这里的处理方式是错误的。正确的语法是什么?
你可以计算 属性:
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
module: {id: 0, plannings: [0,1,2,3,4,5]}
}
},
computed: {
msize() {
return this.module.plannings.length % 2 === 0 ?
6 : this.module.plannings.length % 3 === 0 ?
12 : 12
}
}
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<div id="app">
<v-app>
<v-main>
<v-container>
<v-col cols="12"
:md="msize"
v-for="planning in module.plannings"
:key="planning.id"
>
{{planning}}
</v-col>
</v-container>
</v-main>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
正确的语法是:md="plannings.length % 2 === 0 ? 6 : 12"
演示 :
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: () => ({
plannings: [{
id: 1
}, {
id: 2
}]
}),
});
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css"/>
<div id="app">
<v-row>
<v-col
:md="plannings.length % 2 === 0 ? 6 : 12"
v-for="planning in plannings"
:key="planning.id"
style="background-color: yellow"
>
{{ planning.id }}
</v-col>
</v-row>
</div>
我在 Vue/Vuetify
中的断点上应用条件时遇到困难基本上我想说...如果数组的长度能被2整除,那么MD=6,如果能被3整除那么MD=12
<v-col cols="12"
:md="module.plannings.length % 2 === 0 ? 6 ? module.plannings.length % 2 === 0 ? 12 : 12"
v-for="planning in module.plannings"
:key="planning.id"
>
很明显,我在这里的处理方式是错误的。正确的语法是什么?
你可以计算 属性:
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
module: {id: 0, plannings: [0,1,2,3,4,5]}
}
},
computed: {
msize() {
return this.module.plannings.length % 2 === 0 ?
6 : this.module.plannings.length % 3 === 0 ?
12 : 12
}
}
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<div id="app">
<v-app>
<v-main>
<v-container>
<v-col cols="12"
:md="msize"
v-for="planning in module.plannings"
:key="planning.id"
>
{{planning}}
</v-col>
</v-container>
</v-main>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
正确的语法是:md="plannings.length % 2 === 0 ? 6 : 12"
演示 :
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: () => ({
plannings: [{
id: 1
}, {
id: 2
}]
}),
});
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css"/>
<div id="app">
<v-row>
<v-col
:md="plannings.length % 2 === 0 ? 6 : 12"
v-for="planning in plannings"
:key="planning.id"
style="background-color: yellow"
>
{{ planning.id }}
</v-col>
</v-row>
</div>