访问Vue自定义渲染器中的变量

huangapple go评论102阅读模式
英文:

Access variable in vue custom renderer

问题

我正在尝试在Laravel中实现这个Vue词云组件的自定义渲染器:https://github.com/SeregPie/VueWordCloud

首先,我使用PHP准备我的词组数组(已经工作):

  1. @php
  2. $tags = App\Models\Tag::withCount('songs')->orderBy("songs_count", "desc")->take(50)->get();
  3. $words = "";
  4. foreach($tags as $tag)
  5. {
  6. $count = $tag->countSongs();
  7. $words .= "['$tag->name', $count],";
  8. }
  9. @endphp

然后,我包含了词云(这部分已经工作):

  1. <vue-word-cloud
  2. style="
  3. height: 480px;
  4. width: 80%;
  5. margin: 0 auto;
  6. "
  7. :words="[{{ $words }}]"
  8. :color="([, weight]) => weight > 40 ? 'Red' : weight > 30 ? 'Orange' : weight > 20 ? 'Seagreen' : weight > 10 ? 'White' : weight > 5 ? 'LightSkyBlue ' : 'White'"
  9. font-family="Nunito"
  10. >
  11. </vue-word-cloud>

但是,当我尝试使用自定义渲染器时,像这样(我需要在每个词周围包装一个链接):

  1. <vue-word-cloud
  2. style="
  3. height: 480px;
  4. width: 80%;
  5. margin: 0 auto;
  6. "
  7. :words="[{{ $words }}]"
  8. :color="([, weight]) => weight > 40 ? 'Red' : weight > 30 ? 'Orange' : weight > 20 ? 'Seagreen' : weight > 10 ? 'White' : weight > 5 ? 'LightSkyBlue ' : 'White'"
  9. font-family="Nunito"
  10. >
  11. <template slot-scope="{text, weight, word}">
  12. <div>
  13. @{{ text }} <!-- 这里不起作用,我需要在这里的词周围有一个链接 -->
  14. </div>
  15. </template>
  16. </vue-word-cloud>

它产生以下错误:

[Vue警告]:在渲染期间访问了属性"text",但在实例上未定义。

我绝不是Vue.js专家,也许我错过了一些微不足道的东西。如何访问自定义渲染器的"word"变量?

英文:

I am trying to implement te custom renderer of this Vue Word Cloud component in Laravel:
https://github.com/SeregPie/VueWordCloud

I first prepare my word array using php (working):

  1. @php
  2. $tags = App\Models\Tag::withCount(&#39;songs&#39;)-&gt;orderBy(&quot;songs_count&quot;, &quot;desc&quot;)-&gt;take(50)-&gt;get();
  3. $words = &quot;&quot;;
  4. foreach($tags as $tag)
  5. {
  6. $count = $tag-&gt;countSongs();
  7. $words .= &quot;[&#39;$tag-&gt;name&#39;, $count],&quot;;
  8. }
  9. @endphp

Then I include the word cloud (this is working)

  1. &lt;vue-word-cloud
  2. style=&quot;
  3. height: 480px;
  4. width: 80%;
  5. margin: 0 auto;
  6. &quot;
  7. :words=&quot;[{{ $words }}]&quot;
  8. :color=&quot;([, weight]) =&gt; weight &gt; 40 ? &#39;Red&#39; : weight &gt; 30 ? &#39;Orange&#39; : weight &gt; 20 ? &#39;Seagreen&#39; : weight &gt; 10 ? &#39;White&#39; : weight &gt; 5 ? &#39;LightSkyBlue &#39; : &#39;White&#39;&quot;
  9. font-family=&quot;Nunito&quot;
  10. &gt;
  11. &lt;/vue-word-cloud&gt;

But when I try to use the custom renderer like this: (I need this to wrap a link around every word)

  1. &lt;vue-word-cloud
  2. style=&quot;
  3. height: 480px;
  4. width: 80%;
  5. margin: 0 auto;
  6. &quot;
  7. :words=&quot;[{{ $words }}]&quot;
  8. :color=&quot;([, weight]) =&gt; weight &gt; 40 ? &#39;Red&#39; : weight &gt; 30 ? &#39;Orange&#39; : weight &gt; 20 ? &#39;Seagreen&#39; : weight &gt; 10 ? &#39;White&#39; : weight &gt; 5 ? &#39;LightSkyBlue &#39; : &#39;White&#39;&quot;
  9. font-family=&quot;Nunito&quot;
  10. &gt;
  11. &lt;template slot-scope=&quot;{text, weight, word}&quot;&gt;
  12. &lt;div&gt;
  13. @{{ text }} &lt;!-- this is not working, I need a link around the word here --&gt;
  14. &lt;/div&gt;
  15. &lt;/template&gt;
  16. &lt;/vue-word-cloud&gt;

It produces the following error:

  1. [Vue warn]: Property &quot;text&quot; was accessed during render but is not defined on instance.

I am by no means a VUE.js expert and maybe I am missing something trivial.
How can I access the custom renderer's "word" variable

答案1

得分: 2

v-slot 替换 scope-slot,它就会起作用。
这是参考链接

英文:

Replace scope-slot with v-slot and it will work.
Here is the reference.

答案2

得分: 1

slot-scope 语法在 v2.6 之后已被弃用。请使用 v-slot 替代:

  1. <template v-slot="{text, weight, word}">
英文:

The slot-scope syntax is deprecated since v2.6. Use v-slot instead:

  1. &lt;template v-slot=&quot;{text, weight, word}&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月24日 15:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定