【发布时间】:2020-03-06 02:32:26
【问题描述】:
我查看了其中一个答案并尝试应用它,但后来我意识到它不起作用,我尝试了很多方法,例如使用 Vuex 来计算我使用它之前的年份,但没有任何效果。
Generate Years from 1900 to current year with VueJS
属性或方法“值”未在实例上定义,但在渲染期间被引用。通过初始化该属性,确保此属性是反应性的,无论是在数据选项中,还是对于基于类的组件。请参阅:https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties。
就像我说的,我尝试使用 Vuex 并尝试在 beforeCreate 钩子上调用它,但它不起作用。
<template>
<md-field md-clearable>
<label for="year">Year</label>
<md-select v-model="year" name="year" id="year">
<md-option :v-for="value in values" value="value">
{{ value }}
</md-option>
</md-select>
</md-field>
</template>
<script>
export default {
name: 'Component',
data () {
year: null
},
computed: {
values () {
const year = new Date().getFullYear()
return Array.from({length: year - 1900}, (value, index) => 1901 + index)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
我希望下拉列表具有年份值,而不是为空并给出错误消息。我也尝试重命名这些值,但它不必对此做任何事情。这是一个非常奇怪的错误。我很想知道它是否是由 vue.js 本身引起的。
【问题讨论】:
-
year未定义,您在v-model中使用它 -
我也尝试将其重命名为 myear,但没有成功。
-
是的,你需要定义它。在
data(){ return { myyear: null }}); -
但这并不重要,因为我一开始就把“年”换成了“价值”。
-
我不确定你的意思
标签: vue.js