我有一个Nuxt应用程序,我已经在其中安装了vue-scrollactive作为插件。
我在一个子页面上使用它(如website.com/company)。如果我重新加载website.com/company,插件工作正常--但是,如果我使用nuxt-link从另一个页面(如website.com或website.com/about)加载网站,那么scrollactive插件就不能工作。
更新:这似乎与v-else上出现的fetchState和main标签有关。我使用fetch对虚拟数据进行了测试,它起作用了。
plugins/scrollActive.js
import Vue from 'vue'
import scrollactive from 'vue-scrollactive'
Vue.use(scrollactive)nuxt.config.js
plugins: [{ src: '~/plugins/scrollActive' }],pages/company.vue
<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>发布于 2021-05-17 13:11:47
这有点老生常谈,但我更改了没有v-else的主区域,并将v-if="$fetchState.pending === false"和:class="company === {} ? 'hidden' : 'block'"添加到一个包含数据区域的div中。目前有效,但并不理想。
<!-- 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>https://stackoverflow.com/questions/67531609
复制相似问题