首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue插件在nuxt服务器加载上工作,但在客户端导航上不起作用(vue-scrollactive)

Vue插件在nuxt服务器加载上工作,但在客户端导航上不起作用(vue-scrollactive)
EN

Stack Overflow用户
提问于 2021-05-14 16:53:37
回答 1查看 107关注 0票数 0

我有一个Nuxt应用程序,我已经在其中安装了vue-scrollactive作为插件。

我在一个子页面上使用它(如website.com/company)。如果我重新加载website.com/company,插件工作正常--但是,如果我使用nuxt-link从另一个页面(如website.com或website.com/about)加载网站,那么scrollactive插件就不能工作。

更新:这似乎与v-else上出现的fetchStatemain标签有关。我使用fetch对虚拟数据进行了测试,它起作用了。

plugins/scrollActive.js

代码语言:javascript
复制
import Vue from 'vue'
import scrollactive from 'vue-scrollactive'
Vue.use(scrollactive)

nuxt.config.js

代码语言:javascript
复制
plugins: [{ src: '~/plugins/scrollActive' }],

pages/company.vue

代码语言:javascript
复制
<div class="flex flex-col h-screen bg-gray-100">
    <TheAppNavbar :company="company" />

    <!-- Subnav  -->
    <scrollactive
        :offset="30"
        :highlight-first-item="true"
        scroll-container-selector="#data-area"
        class="block h-16 overflow-x-auto border-b dark:border-gray-600 md:hidden bg-gray-50 dark:bg-gray-800 scrollbars-hidden my-nav"
    >
        <div class="inline-flex items-center h-full text-lg">
            <a
                v-for="category in categories"
                :key="category"
                :href="`#${category}`"
                class="flex items-center h-full text-gray-500 capitalize scrollactive-item dark:text-gray-400'"
            >
                <span v-if="publicCat(category)" class="mx-4 sm:mx-8">{{
                    category
                }}</span>
            </a>
        </div>
    </scrollactive>

    <!-- Bottom section -->
    <div class="flex flex-1 min-h-0">
        <!-- Narrow sidebar-->
        <scrollactive
            :offset="30"
            :highlight-first-item="true"
            scroll-container-selector="#data-area"
            aria-label="Sidebar"
            class="hidden bg-gray-800 dark:bg-white w-28 md:flex md:flex-col md:overflow-y-auto scrollbars-hidden my-nav"
        >
            <div class="relative flex flex-col px-2 mt-4 space-y-2">
                <a
                    v-if="publicCat('sports')"
                    href="#sports"
                    class="flex scrollactive-item"
                >
                    <span class="mt-2">Sports</span>
                </a>
                <a
                    v-if="publicCat('finance')"
                    href="#finance"
                    class="flex scrollactive-item"
                >
                    <span class="mt-2">Finance</span>
                </a>
            </div>
        </scrollactive>

        <!-- Main area -->
        <div v-if="$fetchState.pending">
             Loading
        </div>
        <div v-else-if="$fetchState.error">
            An error occurred :(
        </div>
        <main 
            v-else
            id="data-area"
        >
            <section
                v-for="cat in categories"
                :id="cat"
                :key="cat"
                aria-labelledby="primary-heading"
                class="flex flex-col flex-1"
            >
                <div v-if="company[cat].public === true">
                    <div id="section-header">
                        <h2>
                            {{ cat }}
                        </h2>
                        <p v-if="formattedDate(cat) !== null">
                            Updated:
                            {{ formattedDate(cat) }}
                        </p>
                    </div>

                    <CompanySports
                        v-if="cat === 'sports'"
                        class="pb-20 sm:pt-8"
                        :financials="company.sports"
                    />

                    <CompanyFinance
                        v-if="cat === 'finance'"
                        class="pb-20 sm:pt-8"
                        :financials="company.finance"
                    />

                </div>
            </section>

            <a
                :href="`https://${company.website}`"
                target="_blank"
                >Visit Website
               </a>
        </main>
    </div>
</div>

<script>

...

methods: {
    publicCat(cat) {
        if (this.company[cat]) {
            if (this.company[cat].public === true) {
                return true
            } else {
                return false
            }
        }
    },
    formattedDate(cat) {
        if (this.company[cat].updated) {
            const date = new Date(this.company[cat].updated.seconds * 1000)
            return date.toLocaleDateString('en-US', {
                year: 'numeric',
                month: 'short',
                day: 'numeric',
            })
        }

        return null
    },
},
</script>
EN

回答 1

Stack Overflow用户

发布于 2021-05-17 13:11:47

这有点老生常谈,但我更改了没有v-else的主区域,并将v-if="$fetchState.pending === false":class="company === {} ? 'hidden' : 'block'"添加到一个包含数据区域的div中。目前有效,但并不理想。

代码语言:javascript
复制
<!-- Main area -->
    <div v-if="$fetchState.pending">
         Loading
    </div>
    <div v-else-if="$fetchState.error">
        An error occurred :(
    </div>
    <main 
        id="data-area"
    >
        <section
            v-for="cat in categories"
            :id="cat"
            :key="cat"
            aria-labelledby="primary-heading"
            class="flex flex-col flex-1"
        >
            <div 
                v-if="$fetchState.pending === false && company[cat].public === true"
                :class="company === {} ? 'hidden' : 'block'"
            >
                <div id="section-header">
                    <h2>
                        {{ cat }}
                    </h2>
                    <p v-if="formattedDate(cat) !== null">
                        Updated:
                        {{ formattedDate(cat) }}
                    </p>
                </div>

                <CompanySports
                    v-if="cat === 'sports'"
                    class="pb-20 sm:pt-8"
                    :financials="company.sports"
                />

                <CompanyFinance
                    v-if="cat === 'finance'"
                    class="pb-20 sm:pt-8"
                    :financials="company.finance"
                />

            </div>
        </section>

        <a
            v-if="$fetchState.pending === false"
            :class="company === {} ? 'hidden' : 'block'"
            :href="`https://${company.website}`"
            target="_blank"
            >Visit Website
           </a>
    </main>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67531609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档