vue.js - vue.js 中未定义属性或方法
问题描述
我正在使用 Django 创建一个 Web 应用程序并在模板文件中使用 Vue.js。当我尝试根据准备好的数据显示图标时,出现以下错误。
vue.js:634 [Vue warn]: Property or method "eval" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
vue.js:634 [Vue warn]: Unknown custom element: <star-icon> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
(found in <Root>)
我似乎通过道具设置属性“eval”并将自定义元素放置到位。
html
<div id="stars" data-eval="{{ evaluation }}"> <!-- {{ evaluation }} is Number -->
<ul style="list-style: none;">
<li class="d-inline-block">
<star-icon :eval="eval" data-id="1"></star-icon>
<star-icon :eval="eval" data-id="2"></star-icon>
<star-icon :eval="eval" data-id="3"></star-icon>
<star-icon :eval="eval" data-id="4"></star-icon>
<star-icon :eval="eval" data-id="5"></star-icon>
</li>
</ul>
</div>
javascript
var starIcon = {
props: ["eval"],
delimiters: ['[[', ']]'],
data: function () {
return {
right: false,
star_id: 0,
}
},
mounted: function () {
var elm = this.$el;
var id = elm.getAttribute('data-id');
this.star_id = Number(id);
},
template: '<i :class="star"></i>',
computed: {
star: function () {
if (this.star_id <= this.eval) {
this.right = true;
} else {
this.right = false;
};
return {
fas: this.right,
far: !this.right,
'fa-star': true,
'fa-yellow': true,
'fa-2x': true
};
}
},
}
new Vue({
el: '#stars',
delimiters: ['[[', ']]'],
components: {
'star-icon': starIcon,
},
data: {
eval: 3,
},
beforeMount: function () {
var elm = this.$el;
var evaluation = elm.getAttribute('data-eval');
this.eval = Number(evaluation);
console.log(this.eval);
},
})
最后一行,console.log(this.eval);,返回一个有效的数字,所以这部分似乎工作。
如何解决上述错误?
谢谢你。
解决方案
我不知道原因,但是当我更改脚本导入顺序时,显示工作正常。但是还是有错误...
推荐阅读
- javascript - 如何在画布游戏中设置高度限制?
- r - 使用最大日期基于 ID 合并多列数据框?
- jpa - 如何在内存数据库脚本中为 h2 指定字符编码?
- sql - SQL Group by 以及相同的帐号
- c# - How to use an UWP Application on Raspberry PI to set the SystemTime
- spring - 仅适用于项目文件的 IntelliJ Spring Bean 依赖关系图
- java - Java SSH Ganymed - 尝试使用用户名和密码连接时出现随机错误
- node.js - SyntaxError:意外的令牌
- excel - Excel:跨书,隐式引用所有工作表
- c# - 为数组中的每个项目添加一个按钮