【问题标题】:Why do background images load when navigating by anchor link but not by routerLink?为什么通过锚链接而不是 routerLink 导航时加载背景图像?
【发布时间】: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 那里。如果您需要更多代码示例,请询问。我不想用代码来重载这个问题,但我试图尽可能多地包含而不混乱。
  • 一个允许调试应用程序的可行演示将使其更容易解决。该问题没有显示路由器的配置方式,但我假设&lt;a&gt; 重新加载页面,因此存在差异。由于 Materialize 不是特别为 SPA 和 Vue 构建的,因此可以肯定地假设它初始化了一次视差并且不知道进一步的更改。您可能会注意到它解释了它是如何在老式 JS 中完成的。每次需要初始化视差时,您可能都需要调用parallax()
  • 未经请求的提示:不要在布局中使用换行符。在您的元素 (h1 {}) 或应用边距或填充的类 (.margin-top {}) 上使用全局样式。
  • 浏览器控制台说什么? 404 为那些图像 URL?那么这可能是用于将相对图像 URL 补全为绝对 URL 的不同基本 URL 的问题。
  • @CBroe 浏览器控制台对图像没有任何警告或错误。浏览器控制台唯一会说的是警告“DevTools 无法解析 SourceMap”

标签: javascript html image vue.js parallax


【解决方案1】:

好的,所以这不是 答案,但我需要发布一些代码来说明。

我不得不承认我对 JS 框架并不完全熟悉,但我确实非常了解 materializecss。我一直在尝试帮助另一个用户解决基于 React 的问题,即当使用 react 路由器时 sidenav 停止工作 - 直到他刷新页面,当它再次正常工作时。

所以这是我的预感:

Parallax 是一个需要像这样初始化的组件:

document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('.parallax');
    var instances = M.Parallax.init(elems);
  });

  // Or with jQuery

  $(document).ready(function(){
    $('.parallax').parallax();
  });

如果您忽略该初始化代码,它将无法工作。并且,取自选择字段 dynamically added components need reinitialising 的文档。

您必须初始化选择元素,如下所示。此外, 您需要单独调用任何动态生成的选择 页面生成的元素。

所以我的预感是,当页面通过导航器重新渲染时,我们会在 dom 中添加一个新组件 - 但这是在初始化已经运行之后。所以该组件没有被初始化。

可能是错的,希望我不是为了你而不是为了我。正如我所说,我对 Vue 或 React 不太了解,而且我在每次渲染时运行 init 的建议对于 React 问题不起作用 - 但是有很多活动部件很难排除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2023-02-07
    • 2016-11-18
    相关资源
    最近更新 更多