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

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

Access variable in vue custom renderer

问题

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

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

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

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

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

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

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

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

Then I include the word cloud (this is working)

	&lt;vue-word-cloud
		style=&quot;
			height: 480px;
			width: 80%;
			margin: 0 auto;
		&quot;
		:words=&quot;[{{ $words }}]&quot;
		: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;
		font-family=&quot;Nunito&quot;
	&gt;	
	
	&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)

	&lt;vue-word-cloud
		style=&quot;
			height: 480px;
			width: 80%;
			margin: 0 auto;
		&quot;
		:words=&quot;[{{ $words }}]&quot;
		: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;
		font-family=&quot;Nunito&quot;
	&gt;	
	  &lt;template slot-scope=&quot;{text, weight, word}&quot;&gt;
		&lt;div&gt;
		  @{{ text }} &lt;!-- this is not working, I need a link around the word here --&gt;
		&lt;/div&gt;
	  &lt;/template&gt;		
	&lt;/vue-word-cloud&gt;

It produces the following error:

[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 替代:

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

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

&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:

确定