【发布时间】:2020-06-08 03:53:39
【问题描述】:
我觉得这个问题的答案很简单,但我在任何地方都找不到答案。我正在使用 Vue.js (v2.6.11) 构建一个非常简单的应用程序,它只包含两个页面,一个主页和另一个带有表单的页面。现在,在这两个页面上,都有几个用MaterializeCSS 制作的视差容器。如果您不熟悉,视差容器基本上只是一个以图像为背景的 div,当用户滚动时,背景图像的移动速度与网站前景的移动速度不同。
我的问题是,当我使用 Vue.js 链接 <router-link :to="{name: "FormPage"}>Form Page</router-link> 从主页导航到带有表单的页面时,视差容器中的图像不会加载。但是,如果我刷新页面,图像加载正常,一切都很好。同样,如果我用简单的<a href="/FormPage">Form Page</a> 替换<router-link>,图像加载正常,一切正常。
所以我的问题是:为什么我的视差容器图像没有加载到使用<router-link></router-link> 导航到的页面上,但是当我使用<a href=""></a> 导航到同一页面时,图像会加载?换句话说,<router-link></router-link> 在做什么阻止我的视差容器图像加载??
我们将不胜感激任何和所有反馈。否则,我希望你有一个美好的一天,并感谢你花时间阅读和/或回答我的问题:)
--更新-- 代码: 这是主页上包含相关链接的组件:
<template>
<div id="index-banner" class="parallax-container" style="height: 400px;">
<div class="section no-pad-bot">
<div class="container">
<br><br>
<h1 class="header center white-text">{{ translations.title }}</h1>
<div class="row center">
<h5 class="header col s12 white-text light">{{ translations.subtitle }}</h5>
</div>
<div class="row center">
<!-- <router-link :to="{name: 'Generator', force: true }" class="btn-large waves-effect waves-light teal lighten-1 center-align">{{ buttonText }}</router-link> -->
<a href="/email-signature-generator" class="btn-large waves-effect waves-light teal lighten-1 center-align">{{ translations.buttonText }}</a>
</div>
</div>
</div>
<div class="parallax"><img :src="img" alt="Unsplashed background img 1"></div>
</div>
</template>
<script>
export default {
name: 'Banner1',
props: {
translations: String,
img: String
},
}
</script>
<style>
</style>
这是导航到的“视图”,包含相关视差容器:
<template>
<div>
<PageTitle
:translations="$t('generatorPage.components.pageTitle')"
img="/imgs/parallax5.jpeg"
/>
<SigForm
:translations="$t('generatorPage.components.sigForm')"
/>
<Separator
img="/imgs/parallax5.jpeg"
/>
</div>
</template>
<script>
import PageTitle from '@/components/banner/parallax/PageTitle'
import Separator from '@/components/banner/parallax/Separator'
import SigForm from '@/components/generator/SigForm'
export default {
name: 'Generator',
components: {
PageTitle,
SigForm,
Separator,
}
}
</script>
<style>
</style>
这是带有视差容器的“PageTitle”组件:
<template>
<div class="parallax-container form-parallax-container">
<h1 class="white-text center">{{ translations.title }}</h1>
<div class="parallax"><img :src="img"></div>
</div>
</template>
<script>
export default {
name: 'PageTitle',
props: {
translations: String,
img: String
}
}
</script>
<style>
</style>
这是带有视差容器的“分隔符”:
<template>
<div class="parallax-container form-parallax-container">
<div class="parallax"><img :src="img"></div>
</div>
</template>
<script>
export default {
name: 'Separator',
props: {
img: String
}
}
</script>
<style>
</style>
---更新:完整的回购和沙盒应用程序--- 有人提到如果我提供一个沙盒应用程序或任何调试它会更容易,所以我只是将github repo公开并上传到codesandbox.io。您会注意到,在codesandbox.io 上,视差图像甚至根本没有加载...在主页、表单页面或刷新时!
【问题讨论】:
-
@depperm 那里。如果您需要更多代码示例,请询问。我不想用代码来重载这个问题,但我试图尽可能多地包含而不混乱。
-
一个允许调试应用程序的可行演示将使其更容易解决。该问题没有显示路由器的配置方式,但我假设
<a>重新加载页面,因此存在差异。由于 Materialize 不是特别为 SPA 和 Vue 构建的,因此可以肯定地假设它初始化了一次视差并且不知道进一步的更改。您可能会注意到它解释了它是如何在老式 JS 中完成的。每次需要初始化视差时,您可能都需要调用parallax()。 -
未经请求的提示:不要在布局中使用换行符。在您的元素 (
h1 {}) 或应用边距或填充的类 (.margin-top {}) 上使用全局样式。 -
浏览器控制台说什么? 404 为那些图像 URL?那么这可能是用于将相对图像 URL 补全为绝对 URL 的不同基本 URL 的问题。
-
@CBroe 浏览器控制台对图像没有任何警告或错误。浏览器控制台唯一会说的是警告“DevTools 无法解析 SourceMap”
标签: javascript html image vue.js parallax