我不知道如何在 Vue Js 中为可重用组件创建可更改的文本
P粉957723124
P粉957723124 2023-09-03 22:33:40
0
1
372
<p>我正在通过观看教程创建一个可重用的选项卡组件。看完之后,我明白了它是如何运作的。但是,对于我的项目,我需要创建标题包含可以更改的标题的选项卡,因为这是一个可重用的组件,所以我必须更改每个新选项卡的每个标题的标题,但我还没有弄清楚如何做吧。我需要以某种方式从我添加到页面的组件 TabsWrapper 获取标题</p> <pre class="brush:php;toolbar:false;">&lt;div class=&quot;tab_header&quot;&gt;{{title}}&lt;/div&gt;</pre> <p>然后让此标题更改此 div 内的文本,该 div 是 TabsWrapper 组件的主标头。</p> <pre class="brush:php;toolbar:false;">&lt;div class=&quot;tab_header&quot;&gt;{{title}}&lt;/div&gt;</pre> <p>我的代码: 第一个是我添加到网站主页的组件外代码。</p> <pre class="brush:php;toolbar:false;">&lt;TabsWrapper&gt; &lt;Tab title=&quot;Tab 1&quot;&gt;Hello 1&lt;/Tab&gt; &lt;Tab title=&quot;Tab 2&quot;&gt;Hello 2 &lt;/Tab&gt; &lt;Tab title=&quot;Tab 3&quot;&gt;Hello 3&lt;/Tab&gt; &lt;Tab title=&quot;Tab 4&quot;&gt;Hello 4&lt;/Tab&gt; &lt;/TabsWrapper&gt;</pre> <p>第二个是负责TabsWrapper的组件内部的代码</p> <pre class="brush:php;toolbar:false;">&lt;template&gt; &lt;div class=&quot;tabs&quot;&gt; &lt;div class=&quot;tab_header&quot;&gt;&lt;/div&gt; &lt;ul class=&quot;tabs_header&quot;&gt; &lt;li v-for=&quot;title in tabTitles&quot; :key=&quot;title&quot; @click=&quot;selectedTitle = title&quot; :class=&quot; {selected: title ==selectedTitle}&quot; &gt; {{ title }} &lt;/li&gt; &lt;/ul&gt; &lt;slot /&gt; &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import { ref} from 'vue'; import { provide } from 'vue'; export default{ setup(props,{slots}){ const tabTitles=ref(slots.default().map((tab)=&gt; tab.props.title)) const selectedTitle=ref(tabTitles.value[0]) provide(&quot;selectedTitle&quot;, selectedTitle) return{ selectedTitle, tabTitles, } } } &lt;/script&gt;</pre> <p>这段代码从 Tab 中获取每个标题</p> <pre class="brush:php;toolbar:false;">&lt;Tab title=&quot;Tab 1&quot;&gt;Hello 1&lt;/Tab&gt;</pre> <p>这段代码将其呈现出来</p> <pre class="brush:php;toolbar:false;">&lt;li v-for=&quot;title in tabTitles&quot; :key=&quot;title&quot; @click=&quot;selectedTitle = title&quot; :class=&quot; {selected: title ==selectedTitle}&quot; &gt; {{ title }} &lt;/li&gt;</pre> <p>我尝试过重复相同的技术,但它成功了,但我认为还有更好的方法</p> <p> <pre class="snippet-code-html lang-html prettyprint-override"><code> &lt;div class="tabs"&gt; &lt;div class="tab_header" v-for="headtitle in headTitles" :key="headtitle"&gt;{{headtitle}}&lt;/div&gt; &lt;ul class="tabs_header"&gt; &lt;li v-for="title in tabTitles" :key="title" @click="selectedTitle = title" :class=" {selected: title ==selectedTitle}" &gt; {{ title }} &lt;/li&gt; &lt;/ul&gt; &lt;slot /&gt; &lt;/div&gt; &lt;/template&gt; &lt;script&gt; import { ref} from 'vue'; import { provide } from 'vue'; export default{ setup(props,{slots}){ const tabTitles=ref(slots.default().map((tab)=&gt; tab.props.title)) const headTitles=ref(slots.default().map((tab)=&gt;tab.props.headtitle)) const selectedTitle=ref(tabTitles.value[0]) provide("selectedTitle", selectedTitle) return{ selectedTitle, tabTitles, headTitles, } } } &lt;/script&gt;</code></pre> </p>
P粉957723124
P粉957723124

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!