【问题标题】:How to use GSAP bonus plugins in Nuxt.js?如何在 Nuxt.js 中使用 GSAP 奖励插件?
【发布时间】:2018-12-19 18:40:36
【问题描述】:

我使用https://github.com/nuxt-community/starter-template 进行全新的本地安装。我的依赖是 gsap 2.0.1nuxt 1.0.0

到目前为止,我试图让奖金插件在 Nuxt 中运行:

1 - node_modules 中的所有奖励文件并通过页面文件导入(不工作)

我下载了 bonus-files-for-npm-users 包并将所有文件放在 node_modules/gsap/ 中。

在我的页面文件 index.vue 我这样引用 GSAP:

import { TweenMax } from 'gsap'; 
import { MorphSVGPlugin } from 'gsap/MorphSVGPlugin';
// I also tried:
// import MorphSVGPlugin from 'gsap/MorphSVGPlugin';

TweenMax 运行正常,但插件没有:

S:\Vue\myPath\node_modules\gsap\MorphSVGPlugin.js:14
import { _gsScope } from "gsap/TweenLite.js";
^^^^^^
SyntaxError: Unexpected token import

2 - 根文件夹中的所有奖励文件并通过页面文件导入 Tweenmax 并通过 nuxt.config.js 导入奖励插件(不工作)

我将所有额外的插件文件移到了根目录 /gsap。我将 TweenMax 的导入保留在我的页面文件中,因为它可以正常工作。在我的 nuxt.config.js 中,我尝试了以下配置,我发现在其他项目中大体相似:

    build: {
        extend(config, { isDev, isClient }) {
            if (isDev && isClient) {
                config.module.rules.push({
                    enforce: 'pre',
                    test: /\.(js|vue)$/,
                    loader: 'eslint-loader',
                    exclude: /(node_modules)/
                })
            };
            if (isClient) {
                config.entry.app.push('gsap', '/gsap/MorphSVGPlugin');
                // I also tried:
                // config.entry.app.push('gsap', '~/gsap/MorphSVGPlugin');
            };
        }
    }

3 - 静态文件夹中的所有奖励文件并通过页面文件导入 Tweenmax 并通过 nuxt.config.js 导入奖励插件(有效,但仅适用于一个奖励插件)

我将所有奖励插件移至 /static/gsap/。万岁!我的奖金插件现在正在运行。在 nuxt.config.js 中使用以下配置:

build: {
    extend(config, { isDev, isClient }) {
        if (isDev && isClient) {
            config.module.rules.push({
                enforce: 'pre',
                test: /\.(js|vue)$/,
                loader: 'eslint-loader',
                exclude: /(node_modules)/
            })
        };
        if (isClient) {
            config.entry.app.push('gsap', '~/static/gsap/MorphSVGPlugin');
        };
    }
}

不幸的是,我只有一个这样的奖励插件。这不起作用:

build: {
    extend(config, { isDev, isClient }) {
        if (isDev && isClient) {
            config.module.rules.push({
                enforce: 'pre',
                test: /\.(js|vue)$/,
                loader: 'eslint-loader',
                exclude: /(node_modules)/
            })
        };
        if (isClient) {
            config.entry.app.push('gsap', '~/static/gsap/MorphSVGPlugin');
            config.entry.app.push('gsap', '~/static/gsap/SplitText');
            config.entry.app.push('gsap', '~/static/gsap/DrawSVGPlugin');
            config.entry.app.push('gsap', '~/static/gsap/GSDevTools');
        };
    }
}

我的问题是: 如何让多个 GSAP 奖励插件在 Nuxt.js 中运行?

【问题讨论】:

标签: nuxt.js gsap


【解决方案1】:

折衷解决方案:我只在使用静态资源时运行它。

script: [
  { src: 'gsap/TweenMax.min.js' },
  { src: 'gsap/MorphSVGPlugin.min.js' },
  { src: 'gsap/SplitText.min.js' },
  { src: 'gsap/DrawSVGPlugin.min.js' },
  { src: 'gsap/GSDevTools.min.js' }
]

但我真的很想了解如何将 GSAP 奖励插件与 node_modules 和常规构建过程一起使用。

【讨论】:

【解决方案2】:

创建插件gsap.js:

import gsap from 'gsap'

if (process.client) {
  gsap.registerPlugin()
}

在你的 nuxt.config 中导入插件,正常。

就是这样。

【讨论】:

  • 感谢您的回答!但问题是关于 gsap 2.0.1nuxt 1.0.0。请参阅有关依赖项的介绍。
  • 你试过了吗?它对我来说非常有效,我只是说......
  • 但是 registerPlugin() 方法是在 3.x 中引入的。它甚至在 2.x 中都不存在,还是我错了?你看过讨论吗(greensock.com/forums/topic/17888-importing-plugins-in-nuxtjs)?不,我没有再次尝试,我切换到 v3。我会努力找时间的。
【解决方案3】:

对于从 Google 来到这里的其他人。 GreenSock 的 ZIP 现在包含 src/bonus-files-for-npm-users/umd/CustomEase.js,您可以将其放在本地文件夹中,然后将其导入 Vue 组件中。

import CustomEase from "../vendor/gsap/CustomEase";

现在按预期工作。

【讨论】:

  • 这个问题是关于 Nuxt 的,不是一般的 vue 组件。你能解释一下如何在 Nuxt 中实现这个功能吗?我今天尝试使用新的 ZIP,将它们导入到页面文件中。但仍然没有运气。我在构建过程中遇到依赖错误。
  • 我相信他建议您添加到“资产”文件夹,然后在那里引用它们。结果类似于您的妥协,但您可以直接将它们包含在您的项目中,而不是从 gsap 中获取它们。
猜你喜欢
  • 2016-02-19
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多