admin管理员组文章数量:1353618
So I am relatively new to learning Vue.js. I have gotten to understand most of it so far but I still have trouble with v-if never working. For example...
I would expect these to not show because showProgress is false!
<template v-if="showProgress">
<div id="progressCols" class="md-layout">
<div class="md-layout-item" :class="[{ activeStep: currentStep(1) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(2) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(3) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(4) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(5) }, '.md-elevation-1']"></div>
{{ showProgress }}
</div>
</template>
<script>
export default {
name: 'ProgressCols',
data: function(){
return {
showProgress: false
}
},
methods: {
currentStep(i){
let n = 0;
if (this.$route.params.name == "Ailments") {
n = 1;
} else if (this.$route.params.name == "Effects") {
n = 2;
}
i < n ? true : false
}
}
}
</script>
So I am relatively new to learning Vue.js. I have gotten to understand most of it so far but I still have trouble with v-if never working. For example...
I would expect these to not show because showProgress is false!
<template v-if="showProgress">
<div id="progressCols" class="md-layout">
<div class="md-layout-item" :class="[{ activeStep: currentStep(1) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(2) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(3) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(4) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(5) }, '.md-elevation-1']"></div>
{{ showProgress }}
</div>
</template>
<script>
export default {
name: 'ProgressCols',
data: function(){
return {
showProgress: false
}
},
methods: {
currentStep(i){
let n = 0;
if (this.$route.params.name == "Ailments") {
n = 1;
} else if (this.$route.params.name == "Effects") {
n = 2;
}
i < n ? true : false
}
}
}
</script>
Share
Improve this question
edited Jun 3, 2018 at 2:59
jhpratt
7,13016 gold badges42 silver badges53 bronze badges
asked Jun 3, 2018 at 2:41
Michael PaccioneMichael Paccione
2,8278 gold badges44 silver badges80 bronze badges
3
-
The
template
tags are special, it'll work on anything else though! – ippi Commented Jun 3, 2018 at 2:44 -
You can inline the template in your ponent:
{ template: '<div>Hello</div>', ... }
without any<template>
-tags. So I think the tags are there for mainly two reasons: To help the parser easily find start and end of your template. and to help your IDE with syntax highlighting. You can do<template lang="jade">
when you use other things than html, but that is (probably?) also the only usable attribute on the template-tag. – ippi Commented Jun 3, 2018 at 3:07 - Related: stackoverflow./q/47459404/104380 – vsync Commented Dec 24, 2019 at 21:31
1 Answer
Reset to default 5The v-if
should be on the outermost div
, not template
.
<template>
<div id="progressCols" class="md-layout" v-if="showProgress">
<div class="md-layout-item" :class="[{ activeStep: currentStep(1) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(2) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(3) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(4) }, '.md-elevation-1']"></div>
<div class="md-layout-item" :class="[{ activeStep: currentStep(5) }, '.md-elevation-1']"></div>
{{ showProgress }}
</div>
</template>
<script>
export default {
name: 'ProgressCols',
data: function(){
return {
showProgress: false
}
},
methods: {
currentStep(i){
let n = 0;
if (this.$route.params.name == "Ailments") {
n = 1;
} else if (this.$route.params.name == "Effects") {
n = 2;
}
i < n ? true : false
}
}
}
</script>
Without a v-else
on the template, it doesn't know what to render when false.
本文标签: javascriptvif not working on templateStack Overflow
版权声明:本文标题:javascript - v-if not working on template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743855690a2550818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论