前提・実現したいこと
Djangoでwebアプリを作成しており、template内で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>)
上記のエラーが5つずつでます。
該当のソースコード
html
1<div id="stars" data-eval="{{ evaluation }}"> 2 <ul style="list-style: none;"> 3 <li class="d-inline-block"> 4 <star-icon :eval="eval" data-id="1"></star-icon> 5 <star-icon :eval="eval" data-id="2"></star-icon> 6 <star-icon :eval="eval" data-id="3"></star-icon> 7 <star-icon :eval="eval" data-id="4"></star-icon> 8 <star-icon :eval="eval" data-id="5"></star-icon> 9 </li> 10 </ul> 11</div>
javascript
1var starIcon = { 2 props: ["eval"], 3 delimiters: ['[[', ']]'], 4 data: function () { 5 return { 6 right: false, 7 star_id: 0, 8 } 9 }, 10 mounted: function () { 11 var elm = this.$el; 12 var id = elm.getAttribute('data-id'); 13 this.star_id = Number(id); 14 }, 15 template: '<i :class="star"></i>', 16 computed: { 17 star: function () { 18 if (this.star_id <= this.eval) { 19 this.right = true; 20 } else { 21 this.right = false; 22 }; 23 return { 24 fas: this.right, 25 far: !this.right, 26 'fa-star': true, 27 'fa-yellow': true, 28 'fa-2x': true 29 }; 30 } 31 }, 32} 33 34new Vue({ 35 el: '#stars', 36 delimiters: ['[[', ']]'], 37 components: { 38 'star-icon': starIcon, 39 }, 40 data: { 41 eval: 3, 42 }, 43 beforeMount: function () { 44 var elm = this.$el; 45 var evaluation = elm.getAttribute('data-eval'); 46 this.eval = Number(evaluation); 47 console.log(this.eval); 48 }, 49})
試したこと
beforeMountをmountedにしてみましたが上手くいきませんでした。
該当のソースコードの最後の行のconsole.log(this.eval)には、2という正しい数値が表示されたので、この部分は上手くいっているようです。
必要であれば他の部分のソースコードも載せます。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。