【问题标题】:angular 5+ implement smart-app-banner vanilla JSangular 5+ 实现 smart-app-banner vanilla JS
【发布时间】:2019-01-07 13:52:51
【问题描述】:

我正在尝试在我的应用中使用 smart-app-banner,但由于网络上没有针对它的类型,我不知道如何在 Angular 中使用它。

到目前为止,我的 app.component.ts 中有这个:

import * as smartBanner from "smart-app-banner";
let smartBannerInstance = smartBanner();

constructor() {
    new smartBannerInstance({
        daysHidden: 10, // days to hide banner after close button is clicked (defaults to 15)
        daysReminder: 20, // days to hide banner after "VIEW" button is clicked (defaults to 90)
        // appStoreLanguage: 'us', // language code for the App Store (defaults to user's browser language)
        title: "Title",
        author: "Authot",
        button: "VIEW",
        store: {
            ios: "On the App Store",
            android: "In Google Play"
        },
        price: {
            ios: "FREE",
            android: "FREE"
        }
        // force: 'android' // Uncomment for platform emulation
    });
}

但我还是习惯了

Uncaught TypeError: Cannot set property 'options' of undefined at SmartBanner

我怎样才能做到这一点?

【问题讨论】:

  • 取决于该模块的捆绑方式,您可能需要import smartBanner from "smart-app-banner"; 而不是import * ..
  • 它不是一个模块,它是一个普通的 js 文件,按照你说的做,我得到Uncaught TypeError: smart_app_banner_1.default is not a function at eval
  • 哦,我明白了...但我刚刚意识到你在做let smartBannerInstance = smartBanner(); 然后new smartBannerInstance({ .. 不应该只是new smartBanner({ ... }) 吗?
  • 相同 - Uncaught TypeError: smart_app_banner_1.default is not a function at eval

标签: javascript angular angular-cli smart-app-banner


【解决方案1】:

因此,在 Angular 应用程序中实现一个简单的 JS 库的整个过程是这样的(在这种特定情况下,它适用于大多数库):

首先,我们使用 all 选择器导入库,如下所示:

import * as SmartBanner from "../../node_modules/smart-app-banner/dist/smart-app-banner.js";

然后,我们在 AppComponent 类中声明它,然后在构造函数中对其进行初始化。

SmartBanner: any;

constructor() {
    new SmartBanner({
        daysHidden: 10, // days to hide banner after close button is clicked (defaults to 15)
        daysReminder: 20, // days to hide banner after "VIEW" button is clicked (defaults to 90)
        // appStoreLanguage: 'us', // language code for the App Store (defaults to user's browser language)
        title: "Title",
        author: "Author",
        button: "VIEW",
        store: {
            ios: "On the App Store",
            android: "In Google Play"
        },
        price: {
            ios: "FREE",
            android: "FREE"
        }
        // force: 'android' // Uncomment for platform emulation
    });
}

而且效果很好。

如果这是一个性能问题,请任何有进一步了解的人告诉我。

【讨论】:

  • 我这样实现并得到错误 TypeError: Cannot read property '1' of null
  • 你是否安装了如下插件:npm install --save smart-app-banner?
  • 当然。它在windows上运行良好,android上有一个错误
猜你喜欢
  • 2013-10-11
  • 2012-10-16
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2018-08-09
  • 1970-01-01
相关资源
最近更新 更多