【问题标题】:Using Smooth Scroll Polyfill with Angular 2 CLI在 Angular 2 CLI 中使用 Smooth Scroll Polyfill
【发布时间】:2017-04-03 01:29:18
【问题描述】:

谁能指导如何在 Angular 2 CLI 中使用 Smooth Scroll Polyfill。

我尝试在 polyfills.ts 中添加以下内容,但它在工作需要时引发错误

require('smoothscroll-polyfill').polyfill();

然后我尝试添加

import 'smoothscroll-polyfill';

虽然它在构建过程中没有抛出任何错误,但是当我在浏览器中运行项目时,它会在控制台上显示以下错误:

ERROR TypeError: Failed to execute 'scroll' on 'Window': No function was found that matched the signature provided.

【问题讨论】:

    标签: angular angular-cli


    【解决方案1】:

    这是 Angular 2/4 解决方案:

    安装smoothscroll-polyfill - npm install smoothscroll-polyfill

    然后添加到你的 src/polyfills.ts 文件中:

    import smoothscroll from 'smoothscroll-polyfill/dist/smoothscroll.js';
    smoothscroll.polyfill();
    

    【讨论】:

    • 谢谢 Stas,对我来说这是可行的: import { polyfill } from 'smoothscroll-polyfill'; polyfill();
    • 我更喜欢这种方法,因为它有助于将 polyfills 排除在我的主代码之外并满足我的关注点分离。正如其他人所建议的那样,也可以安装 @types/smoothscroll-polyfill 以保持 linter 快乐,但这不是必需的。
    【解决方案2】:

    你需要安装 polyfill 和 @types

    1. yarn add smoothscroll-polyfillnpm install smoothscroll-polyfill
    2. yarn add @types/smoothscroll-polyfillnpm install @types/smoothscroll-polyfill

    因此,在您的代码中,您应该在组件中初始化 polyfill 或为您希望使用此 polyfill 提供服务:

    import * as smoothscroll from "smoothscroll-polyfill";
    
    @Component({
      selector: 'app-my-demo',
      templateUrl: './app-my-demo.html',
      styleUrls: ['./app-my-demo.css']
    })
    export MyClass my implements OnInit {
    
      constructor(
      ) {
        smoothscroll.polyfill();
       }
    

    你可以在组件中使用,例如:

      clickAnyThing(event:any){
        window.scroll({ top: 0, left: 0, behavior: 'smooth' });
      }
    

    【讨论】:

    • 我更喜欢@Stas 的解决方案,因为我宁愿将 polyfills 排除在我的主代码之外。我相信polyfills.ts 文件存在于从 CLI 设置的 Angular2+ 项目中。
    【解决方案3】:

    这就是我在polyfills.ts 文件中的设置方式:

    import smoothscroll from 'smoothscroll-polyfill/dist/smoothscroll';
    smoothscroll.polyfill();
    

    那么你可以这样使用它:

    your_element.scrollIntoView({ behavior: 'smooth' });
    

    【讨论】:

    • 这曾经对我有用,但最近坏了。现在我必须像@caballerog 建议的那样在组件中执行此操作。
    【解决方案4】:

    如果您使用的是角度剪辑

    首先安装包:

    npm install smoothscroll-polyfill

    然后将以下内容添加到apps.scripts数组下的angular-cli.json中:

    "../node_modules/smoothscroll-polyfill/src/smoothscroll.js"

    然后尝试类似:

    window.scroll({ top: 400, left: 0, behavior: 'smooth' });

    它应该可以工作。

    【讨论】:

    【解决方案5】:

    #选项 1

    可以直接包含在index.html中,它会立即运行,不需要smoothscroll.polyfill();调用

    <script src="https://unpkg.com/smoothscroll-polyfill@0.4.3/dist/smoothscroll.min.js"></script>
    

    #选项 2

    npm install smoothscroll-polyfill --save

    将其包含在src/polyfills.ts

    import 'smoothscroll-polyfill';

    或将其添加到.angular-cli.json 中的scripts 应该可以。


    在组件中导入:

    import  { polyfill } from "smoothscroll-polyfill"
    
    polyfill()
    
    • #use:

    window.scroll({ top: 200, left: 0, behavior: 'smooth' });


    【讨论】:

    • 将其放在.angular-cli.json 中的scripts 中被认为是更好的做法,从node_modules 加载它。
    • 感谢您的回复,但由于 Angular cli 项目中有一个名为 polyfills.ts 的文件,我认为应该在此文件中添加 polyfills。我遇到了这个stackoverflow.com/a/42982811/2755616,但它似乎不起作用。
    • @user663031 它不再被认为是角 polyfills 的最佳实践。见stackoverflow.com/a/46534123/1438576
    【解决方案6】:

    如果你将它与 Typescript 一起使用,那么你应该安装

    npm install smoothscroll-polyfill @types/smoothscroll-polyfill

    yarn add smoothscroll-polyfill @types/smoothscroll-polyfill/

    然后你可以像下面这样使用它

        import * as smoothscroll from "smoothscroll-polyfill";
        smoothscroll.polyfill();
    
    

    或者, 您可以仅在需要时包含 polyfill。如果您使用的是 webpack、parcel 或任何其他支持动态导入的构建系统,您可以尝试以下方法。

        if (!'scrollBehavior' in document.documentElement.style) {
        // Wait until the Polyfills are loaded
        Promise.all([
            import('smoothscroll-polyfill'),
            import('smoothscroll-anchor-polyfill')
        ])
            // then use the modules however you want
            .then(([smoothscrollPolyfill, smoothscrollAnchorPolyfill]) => {
            // (Unlike this package, smoothscroll-polyfill needs to be actively invoked: )
            smoothscroll.polyfill();
            });
        }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多